0

我需要将处理后的文件移动到新文件夹,我们需要为移动的文件添加时间戳作为后缀。在一天之内,我们可能会多次收到相同的文件。我是批处理脚本的新手。

例如:

Source Folder: 

C:\SourceFiles 

a.xml, 
b.xml,
c.xml 

When I copy/move the files to the destination folder, this is how they need to look as below

Destination Folder: 

C: \DestinationFiles

a_08082013_10_16.xml, 
b_08082013_10_16.xml,
c_08082013_10_16.xml

任何帮助将不胜感激。

4

1 回答 1

0

这可能会有所帮助:

for /f "tokens=*" %%n in ('date /t') do set thisdate=%%n
:: replace '/' with '-' as /'s don't work in file names.
set thisdate=%thisdate:/=-%
for /f "tokens=*" %%n in ('time /t') do set thistime=%%n
:: replace ':' with '-'
set thistime=%thistime::=-%
set firstfolder=c:\first_folder
set secondfolder=c:\second_folder

for /r "%firstfolder%" %%n in (*) do call :movefile "%%~nn" "%%~xn" "%%~dpn"
goto :EOF

:movefile
move "%~3\%~1%~2" "%secondfolder%\%~1_%thisdate%-%thistime%-%random%%~2"

注意:日期和时间命令的输出取决于 Windows 日期/时间设置。您无法从提示中控制它。如果您需要以编程方式控制输出,最好使用另一种语言(例如 vbscript)或第三方实用程序(例如Windows 版本的 GNU date 命令)。

不要将第二个文件夹放在第一个文件夹下,因为“for /r”会从 first_folder 开始遍历整个子树,并将找到的任何文件移动到第二个文件夹,并在此过程中重命名它们。

我们在末尾添加一个随机值,以处理同时处理的重复项。

如果有多个子文件夹,“移动”不会创建新文件夹。在这种情况下,您可能更喜欢 robocopy 或 xcopy(在提示符下键入 robocopy /? 或 xcopy /? 以获得这些命令的帮助)。以上所做的是将输入文件夹的所有子文件夹中的所有内容移动到一个输出文件夹。

于 2013-08-09T15:08:13.720 回答