【问题标题】:Copy the file from one folder to the other and add a time stamp as a suffix when copying batch script将文件从一个文件夹复制到另一个文件夹,并在复制批处理脚本时添加时间戳作为后缀
【发布时间】:2013-08-10 11:19:49
【问题描述】:

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

例如:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: batch-file copy move


    【解决方案1】:

    这可能会有所帮助:

    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 version of GNU date command)。

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多