当文件名是变量时,用于重命名文件的批处理文件

时间:2015-11-25 19:38:53

标签: windows batch-file

我已经找到了这里的基本代码并且正在使用它移动然后重命名我知道文件名的文件。但是,现在我想移动文件名为参数的文件。

当我运行此代码时,它会使用当前日期和时间重命名该文件夹。如何让它重命名文件夹中的变量文件名?我添加了暂停来尝试解决发生的事情。

Echo Off
SET SiteName=%~1
SET FileDate=%~2
SET File=%SiteName%%FileDate%  


IF Not EXIST C:\Users\Administrator\Documents\~Withdrawal_Report\%File%  GOTO DoNothing

copy C:\Users\Administrator\Documents\~Withdrawal_Report\%File%    C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% 


ren C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% %SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.csv
ren C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% %SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.csv


del C:\Users\Administrator\Documents\~Withdrawal_Report\%File%
pause

:DoNothing
Exit

1 个答案:

答案 0 :(得分:0)

尝试使用双引号,如下所示:

Echo Off
SET "SiteName=%~1"
SET "FileDate=%~2"
SET "File=%SiteName%%FileDate%"


IF Not EXIST "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%" GOTO DoNothing

copy "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%" "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" 

ren "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" "%SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.csv"
ren "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" "%SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.csv"


del "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%"
pause

:DoNothing
Exit

另外,据我所知,您尝试重命名同一个文件两次。那之前有用吗?