使用Batch变量重命名文件 [英] Renaming file with Batch variable
问题描述
我有以下代码:
set DATE=%date%
echo %DATE%
set DAY=%DATE:~0,2%
echo %DAY%
rename file09.txt file%DAY%09.txt
pause
应该重命名一个文本文件,并将该月中的某天放在文件名中.但是,我在rename
命令上遇到语法错误.
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.
我认为问题出在将变量插入文件名中.任何帮助,将不胜感激. echo
仅在程序中供我自己参考.
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.
推荐答案
如前所述-您粘贴的代码在许多行上都有尾随空格,而空格通常很重要.之所以发布此消息,是因为您已重用了系统DATE变量名,这会引起问题,并向您显示引号通常很有用-长文件名需要使用引号.
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
这篇关于使用Batch变量重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!