将日期/唯一标识符附加到文件&移动它

时间:2012-11-06 11:04:46

标签: windows batch-file

我开发了一个简单的批处理文件,我想将其设置为移动文件的计划任务。

目前,这是我的代码;

move /-y "C:\Folder\Folder\Folder\*File*.csv" "C:\Folder\Folder\Folder\Folder\File.csv"

pause

然而,这显然会导致重复(以及第二次转移后的覆盖冲突)

如何附加日期(文件只会每天移动一次)或唯一标识符来解决此问题?

由于

3 个答案:

答案 0 :(得分:5)

尝试

 move /-y "C:\Folder\Folder\Folder\*File*.csv" "C:\Folder\Folder\Folder\Folder\File_%date:~6,4%_%date:~3,2%_%date:~0,2%.csv" 

注意:上述命令中的数字6,4 3,2 0,2取决于您的日期格式。检查控制面板(或使用echo %date%)以获取默认日期格式。当然,你可以改变顺序: - )

我的默认日期格式为DD / MM / YYYY&此代码段将其更改为YYYY_MM_DD

答案 1 :(得分:1)

您可以执行以下操作,将日期时间附加到文件名...

for / f“tokens = 1-5 delims = /”%% d in(“%date%”)重命名“C:\ Folder \ Folder \ Folder \ Folder \ File.csv”%% e-% %F - %% g.csv

测试 - 在桌面上创建的test.txt运行.bat(当然,更改了文件名和位置以匹配我的测试文件在.bat文件中的名称和位置。)

运行.bat后,文件名更改为11-06-2012.txt。

希望这会有所帮助。

答案 2 :(得分:1)

问题很简单,但批量解决方案很难看。例如,上面列出的答案之一为我生成了以下输出:

C:\>echo %date:~6,4%_%date:~3,2%_%date:~0,2%
/08/_ 1_Th

显然,它并不总是有效。幸运的是,Rob Van der Woude准备并收集了解决方案such as this。该代码为我生成以下输出:

DateParsed=20121108

我复制了Rob的代码,并在此处引用了他的来源以方便使用。

:: One of the ugliest scripts required for such a seemingly simple thing is getting dates parsed
:: http://www.robvanderwoude.com/datetimentparse.php
SET Today=%Date: =0%
SET Year=%Today:~-4%
:: Include 1 extra character, which will be either a leading zero or a trailing separator
SET Month=%Today:~-10,3%
:: Remove separator
SET Month=%Month:-=%
SET Month=%Month:/=%
:: Clear leading zeroes

SET /A Month = 100%Month% %% 100
:: And add one again, if necessary
SET /A Month = 100 + %Month%
SET Month=%Month:~-2%
SET Day=%Today:~-7,2%
:: Remove separator
SET Day=%Day:-=%
SET Day=%Day:/=%
:: Clear leading zeroes, as there may be 2 leading zeroes
SET /A Day = 100%Day% %% 100
:: And add one again, if necessary
SET /A Day = 100 + %Day%
SET Day=%Day:~-2%

SET DateParsed=%year%%month%%day%
echo DateParsed=%DateParsed%
相关问题