Windows command line-Replace multiple characters in single string

4

1

I apologize if the question title is a bit confusing, but I wanted to keep it short.

Basically what I want to do is take the system date format (%DATE%) and replace characters that would be bad in the command or illegal in file names; for example, spaces and slashes.

The environment I'm doing this in is limited, however. This is running through a service that performs a single line of commands on the client's machine. This means that I can't use variables (to my knowledge) to do it in multiple steps, as everything I've tried is unable to see a variable created on the same line (when using & and && to link commands together). Batch files are also pretty much out of the question, as it could be a struggle to get some of our clients to put a batch file on their system.

Right now, I have the command set up like the following:

MOVE C:\folder\file.csv C:\archive\^"file%DATE:/=-%.csv^"

I was wondering if there was some way to have multiple character replacements in the %DATE:/=-% part.

Currently, the quotes are fixing issues with spaces in the format, and I'm replacing slashes with dashes. I'm hoping that this will be enough to fix any issues, as I don't know why they would have any other illegal characters in their date format, but I figured I'd ask to be prepared in case we have another issue in the future. Feel free to ask for clarification on anything that may be confusing.

Dominator_101

Posted 2012-09-11T16:14:08.227

Reputation: 43

Answers

1

First, dates are inherently difficult in 'pure' cmd, as output is dependent on short date format. That means you may have it output as dd/mm/yyyy; yyyy-dd-mm; mm dd yy etc. Normally I would advise to use either powershell or wmic, but they are probably out of a question in your case, so two possible tricks which may help you:
a) to set and read variable on same line, use call* - note that a must be unset before executing for this to work properly:
set "a=10" & call echo %a%
b) To strip either of several possible separators (but only single ones, and you must know how many there will be) you could use for /f:
for /f "tokens=1-3 delims=-/" %F in ("%DATE%") do echo %F%G%H

*(note: if you have control on how cmd is started, using cmd /v:on enables so called delayed expansion, so you could use set "a=10" & echo !a!)

wmz

Posted 2012-09-11T16:14:08.227

Reputation: 6 132

Yeah, I knew it was based on short date, which is why I wanted to replace multiple things to account for potentially any case that they might have so we don't need to set it per client. I had looked into delayed expansion, but we don't control the start, just the command that is sent. I'll try some tests with the same line variable stuff and get back to you. Thanks! – Dominator_101 – 2012-09-11T18:23:14.220

@Synetech I believe you cannot do it ouside of batch context, see here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/setlocal.mspx?mfr=true Remarks: When you use setlocal outside of a script or batch file, it has no effect.

– wmz – 2012-09-11T18:42:16.103

@wmz That was the understanding I got when I looked into it. For the same line variables, though, it seems to work like I wanted. I was hoping there was some easy way to do it in one set of %%, but this solution still gets the job done. Thanks for the help! – Dominator_101 – 2012-09-11T18:47:35.053

@wmz: I can't get set a=10 & call echo %a% to work without delayed environment expansion enabled.  However, cmd /v:on /c "set a=42 & echo !a!" seems to work. – Scott – 2012-09-11T19:02:01.837

@scott a must be unset before you execute (otherwise it will get expanded with old value), I added it to answer (I also added "" to set to avoid adding space to value. What exact error you have? – wmz – 2012-09-11T19:15:57.787

@wmz: (1) No error; it just expands to the old value; e.g., set a=9 (Enter) set a=11 & call echo %a% (Enter) produces 9. (2) When (and how) is set a=10 different from set "a=10"? – Scott – 2012-09-11T19:47:10.257

@Scott In this case using a constant value being set, you are correct that the quotes don't do anything different in this case. If you were doing set a=%DATE%, though, that might come out as 'set a=1 1 2012', which causes the command to fail. 'set a="1 1 2012"' will work. – Dominator_101 – 2012-09-11T20:01:50.493

yes it's not ideal - the value may not be set previously - you may manipulate only newly created variable. Do set a= and then try with and without call (unsetting a with a= each time). As for (2), set a=10 & (which I did mechanically and spotted only later) creates variable a with value 10<space>. Nasty error that's why it's better to use "" to avoid it. – wmz – 2012-09-11T20:16:46.003

It does work on the same line without unsetting, however. For example, 'set f=%DATE: =-%& call set f=%f:/=-%& call echo %f:=-%.txt' will correctly remove all spaces, slashes, and backslashes and replace them with dashes (assuming f wasn't set BEFORE the whole line was run). – Dominator_101 – 2012-09-11T20:22:08.010

@Dominator_101 yes we are saying the same thing. It cannot be set BEFORE. Otherwise it's old value will be used (it will get expanded on line parse), that's what Scott was reporting. – wmz – 2012-09-11T20:31:30.500