Windows 将日期/唯一标识符追加到文件并将其移动

Windows 将日期/唯一标识符追加到文件并将其移动,windows,batch-file,Windows,Batch File,我开发了一个简单的批处理文件,我想将其设置为移动文件的预定任务 目前,这是我的代码 move /-y "C:\Folder\Folder\Folder\*File*.csv" "C:\Folder\Folder\Folder\Folder\File.csv" pause 但是,在第二次传输之后,这显然会导致重复和覆盖冲突 如何附加一个日期文件每天只移动一次或唯一标识符以解决此问题 谢谢您可以这样做,将日期时间附加到文件名中 对于%date%中的/f令牌=1-5 delims=/%%d,请重命

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

目前,这是我的代码

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

pause
但是,在第二次传输之后,这显然会导致重复和覆盖冲突

如何附加一个日期文件每天只移动一次或唯一标识符以解决此问题


谢谢

您可以这样做,将日期时间附加到文件名中

对于%date%中的/f令牌=1-5 delims=/%%d,请重命名C:\Folder\Folder\Folder\Folder\File.csv%%e-%%f-%%g.csv

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

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


希望这有帮助。

您可以执行类似的操作,将日期时间附加到文件名中

对于%date%中的/f令牌=1-5 delims=/%%d,请重命名C:\Folder\Folder\Folder\Folder\File.csv%%e-%%f-%%g.csv

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

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

希望这有帮助。

试试看

 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\u MM\u DD

试试看

 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\u MM\u DD

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

C:\>echo %date:~6,4%_%date:~3,2%_%date:~0,2%
/08/_ 1_Th
DateParsed=20121108
显然,它并不总是有效的。幸运的是,Rob Van der Woude已经准备并收集了解决方案。该代码为我生成以下输出:

C:\>echo %date:~6,4%_%date:~3,2%_%date:~0,2%
/08/_ 1_Th
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%

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

C:\>echo %date:~6,4%_%date:~3,2%_%date:~0,2%
/08/_ 1_Th
DateParsed=20121108
显然,它并不总是有效的。幸运的是,Rob Van der Woude已经准备并收集了解决方案。该代码为我生成以下输出:

C:\>echo %date:~6,4%_%date:~3,2%_%date:~0,2%
/08/_ 1_Th
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%