0

Can someone please help me figure out the syntax to add the time to an output file's name? I was able to get the date, but the time isn't working for me. I keep googling it but so far I have come up empty. I have figured out how to add it to the file itself, but I want to be able to see it in the name.

The code below, is just a super simple robocopy command that I have saved as test.cmd.

Everything works, everything except adding the time.

Feel free to steal it if you want. Just make sure you Google the switches so that you know what you're doing. That part is easy to find.

My Code

@ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\%date:~-10,2%-%date:~-7,2%-%date:~-4,4% Time.txt" /MT:4
exit
Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

0

I have figured it out!

Here it is if someone else needs it.

@ECHO OFF

if not exist "C:\Example" mkdir "C:\Example"

ROBOCOPY.EXE C:\Example Source "\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\date %date:~-10,2%-%date:~-7,2%-%date:~-4,4% time %time:~0,2%.%time:~3,2%.txt" /MT:4

exit
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    While testing, I noticed a problem. If the time changes then so does the log file. I may need to work on having it grab the time in the beginning and using that as a var throughout? – Ryan Mc Dec 31 '20 at 03:23
  • You should never do it like this anyhow, imagine what would happen if, in this case the variable `%time%` is parsed just as the hour changes, from `13:59` to `14:00` your first instance `%time:~0,2%` may resolve to `13` but in the fraction of time before parsing `%time:~3,2%`, it may resolve to `00`, which means you could essentially be an hour out! It makes more sense to grab it all at once, then manipulate it as needed afterwards. *The same issue could feasibly affect the date too.* – Compo Dec 31 '20 at 16:39