假设我有两个目录:N:\test1 and N:\archive
。
我想要将test1中的所有.csv文件移动到存档。
我想归档这些文件,因为它们现在正在被覆盖,所以我想添加一个时间戳。
到目前为止,我遵循了某种类似的逻辑,但不确定如何将时间戳附加到文件名中:
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/挂载的共享驱动器
发布于 2019-11-03 12:37:15
下面是我们最终的做法:
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
https://stackoverflow.com/questions/58331116
复制相似问题