Renaming file with Batch variable
Solution 1
As has already been stated - your code as pasted had trailing spaces on many lines and the spaces often matter. I posted this because you had reused the system DATE variable name and that causes issues, and to show you that quotes are often helpful - and needed with long filenames.
set "D=%date%"
echo "%D%"
set "DAY=%D:~0,2%"
echo "%DAY%"
rename "file09.txt" "file%DAY%09.txt"
pause
Solution 2
you might have trailing spaces in the set
command. Just try this:
set "DAY=%DATE:~0,2%"
btw. with set DATE=%date%
you doesn't create a new variable. Variables must have case insensitive unique Names.
androidNewbie
Updated on October 26, 2020Comments
-
androidNewbie over 3 years
I have the following code:
set DATE=%date% echo %DATE% set DAY=%DATE:~0,2% echo %DAY% rename file09.txt file%DAY%09.txt pause
It is supposed to rename a text file and put the day of the month in the file name. I am however getting a syntax error on the
rename
command.I think the problem is in inserting the variable into the file name. Any help would be appreciated. The
echo
s are just in the program for my own reference.