假设我有两个目录: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 /挂载共享驱动器
这是我们最终完成此操作的方式:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句