I have a script that does a simple XCOPY routine for backing up all our corporate files:
@echo off
IF %time:~0,2% GTR 7 (
IF %time:~0,2% LSS 21 (
XCOPY "R:\Sage Src" "S:\lastdata" /D /Y /E /R /K /C /H /I >> S:\sageBackupLog-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%-hour%time:~0,2%.txt
XCOPY "R:\importantStuff" "V:\lastdata" /D /Y /E /R /K /C /H /I
EXIT /B 0
)
)
But ever since wrapping it all in the two IF statements, it no longer outputs the hour.
I have tried set %hour%=%time:~0,2% but it doesn’t work, returning an invalid parameters error.
There are two main problems here.
First of all, change your
setcommand into this:set hour=%time:~0,2%(hourwithout enclosing%-signs)Next thing is, that times with a single digit hour value like 9:00 o’clock will be printed like:
with a leading space in front of the first digit. This is the reason for your error message, as your file name might resolve to something like:
This actually forms two parameters as the name is not enclosed with
"To overcome this and print a leading zero for these times, you can use a second
setstatement doing a string replacement, changing spaces to zeros:You can then safely use
%hour%instead of%time:~0,2%in your script.Hope that helps.