如何在文件名后附加时间戳?

时间:2019-10-10 21:07:49

标签: batch-file

假设我有两个目录:N:\test1 and N:\archive

我想将任何.csv文件从test1移至存档。

我想存档文件,因为它们已经被覆盖,所以我考虑添加时间戳。

到目前为止,我正在遵循类似的逻辑,但是不确定如何将时间戳附加到文件名:

REM TimeStamp
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d%%e%%f

REM move the file
move /Y N:\test1\*.csv N:\archive

REM Rename the file with timestamp
ren N:\archive\*.csv *_%DateNtime%.csv

这是我停留的部分:ren N:\archive\*.csv *_%DateNtime%.csv

您可以想象,第一次将存在现有的.csv存档文件,那么如何告诉它重命名刚刚移动的文件?

例如,假设test1文件夹中有file1.csv。将其移至存档后,应将其重命名为file1_%DateNtime%.csv

注意:N:\是UNC /已安装的共享驱动器

1 个答案:

答案 0 :(得分:0)

这是我们最终完成的方式:

SET today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%%TIME:~9,2%
SET today=%today: =0%

REM the second SET today was required because there was some weird execution with the 
REM timestamp happening at 8:00 am. something wrong with the formatting so the files were 
REM never getting timestamped at that time, but by adding that line the issue was resolved.

copy /y N:\test1\file1.csv N:\archive\file1_%today%.csv