批处理脚本移动具有多个方案的文件

时间:2014-10-30 00:40:47

标签: batch-file batch-processing

我有一个自动化流程,使用批处理文件将一个5个文件的集合一次一个地移动到特定的顺序,然后移动到临时目录,然后在继续下一个文件之前对该文件执行操作。该过程是成功的,直到出现如下的新场景。

新方案是随机的 - 通常是星期一 - 5个文件中每个文件都有多次出现(通常在这种情况发生时有2组)。我需要弄清楚的是如何按顺序移动它们 - 即上述动作 - 以便在移动set 2文件之前一次一个地移动set 1中的文件。

如何命名这些文件的示例如下:

收到以下文件样本列表的日期为20141028。

Set 1
file_asia1.txt.20141026
file_asia2.txt.20141026
file_euro.txt.20141024
file_lamr.txt.20141024
file_namr.txt.20141024

Set 2
file_asia1.txt.20141027
file_asia2.txt.20141027
file_euro.txt.20141026
file_lamr.txt.20141026
file_namr.txt.20141026

我需要移动第1组中的每个文件,然后继续设置2.我试图研究这个并且找不到满足上述要求的任何实例。

请帮忙!

1 个答案:

答案 0 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Configure paths
    set "sourceFolder=c:\some\where"
    set "targetFolder=d:\other\somewhere"

    rem Configure the required files in the correct order
    set fileSet="file_asia1.txt" "file_asia2.txt" "file_euro.txt" "file_lamr.txt" "file_namr.txt"

:loop
    rem Check it there is a file for each defined element in the set. If not, leave 
    for %%a in (%fileSet%) do dir /b /a-d "%sourceFolder%\%%~a*" >nul 2>nul || goto :eof

    rem We have files, continue the process
    rem For each file in the set, find the first matching one and process it
    for %%a in (%fileSet%) do (
        set "first="
        for /f "delims=" %%b in ('
            dir /b /a-d /on "%sourceFolder%\%%~a*"
        ') do if not defined first (
            set "first=1"
            echo processing %%~b
            move /y "%sourceFolder%\%%~b" "%targetFolder%" >nul
            rem Do what needs to be done
            rem ....
        )
    )

    rem Go test for another set of files
    goto :loop