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.


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!)

  • 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 Sep 11 '12 at 18:23
  • @Synetech I believe you cannot do it ouside of batch context, see here: microsoft.com/resources/documentation/windows/xp/all/proddocs/… Remarks: When you use setlocal outside of a script or batch file, it has no effect. – wmz Sep 11 '12 at 18:42
  • @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 Sep 11 '12 at 18:47
  • @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 Sep 11 '12 at 19:02
  • @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 Sep 11 '12 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.