【问题标题】:Batch script to move files that match another set of files? [closed]移动与另一组文件匹配的文件的批处理脚本? [关闭]
【发布时间】:2020-02-20 12:01:12
【问题描述】:

这对我有很大的帮助。

示例: Folder1 有 900 个以 .zip 结尾的文件。 Folder2 有 300 个文件与 Folder1 中的文件匹配,但在 .doc 中

我如何创建一个批处理脚本来将文件从 Folder1 移动到 Folder1-sorted 并且只有一个 .doc 替代品最终以 folder1-sorted 仅包含 300 个 .zip 文件?

我最初的猜测是执行 dir > list.txt 并更改每一行的扩展名并使用此列表进行复制。但我想可能有更聪明的解决方案来执行此操作。

【问题讨论】:

    标签: windows batch-file move


    【解决方案1】:

    借助 Setlocal EnableDelayedExpansionFor 循环,您可以使用 ~n 变量修饰符构建一个索引数组,其值仅包含 .doc 文件的文件名,然后在 .zip 上使用另一个 for 循环使用嵌套的For /L 循环与数组进行比较的文件。

    EG:

    @ECHO OFF & Setlocal EnableDelayedExpansion
    CD "your path for parent directory containing folders 1 and 2"
    
    REM build the array using the smaller data set...
    
    PUSHD Folder2
    Set "_I=0"
    For %%A in (*.doc) Do (
        Set /A _I+=1
        Set "file[!_I!]=%%~nA"
    )
    POPD
    PUSHD Folder1
    For %%A in (*.zip) Do (
        For /L %%B in (1,1,!_I!) Do (
            If "!file[%%B]!"=="%%~nA" (
                ECHO(%%A matches !file[%%B]!.doc
    REM command for on Match here
            )
        )
    )
    POPD
    
    

    【讨论】:

    • 在这个例子中我应该把什么移到按文件夹排序的? move %%A C:\somewhere\folder1-sorted?
    • 如果打算移动匹配的 .zip 文件,请使用带有 on match 注释的行中的 %%A 变量来处理匹配的 zip 文件
    • 知道了!非常感谢您按预期工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多