【问题标题】:How to move a long list of files to other directories?如何将一长串文件移动到其他目录?
【发布时间】:2018-01-28 10:37:08
【问题描述】:

我有一个 *.txt 文件,其中包含很长的要移动的文件列表(大约 30k),其中每一行包含要移动的文件的文件名和完整路径,目标文件夹以完整路径分隔通过字符串 to 。

列表文件示例:

C:\USER\BDG\anto\12.jpg to D:\USER\BDG\,
C:\USER\SMG\kent\311.jpg to D:\USER\SMG\,
C:\USER\JKT\lydia\13121.jpg to D:\USER\JKT\,
C:\USER\NYC\tiffany\1e1b1.jpg to D:\USER\NYC\,
C:\USER\MNC\regent\1eb1be1.jpg to D:\USER\MNC\,
etc.

如何逐行处理这个列表文件将所有文件移动到指定文件夹?

【问题讨论】:

    标签: batch-file cmd move


    【解决方案1】:

    最简单的方法是在支持 Perl 正则表达式查找/替换的文本编辑器中打开列表文件,然后运行 ​​Perl 正则表达式将文件顶部的所有内容替换为搜索字符串 ^(.+?) to (.+),$ 并替换字符串 @move "\1" "\2" .结果应该是:

    @move "C:\USER\BDG\anto\12.jpg" "D:\USER\BDG\"
    @move "C:\USER\SMG\kent\311.jpg" "D:\USER\SMG\"
    @move "C:\USER\JKT\lydia\13121.jpg" "D:\USER\JKT\"
    @move "C:\USER\NYC\tiffany\1e1b1.jpg" "D:\USER\NYC\"
    @move "C:\USER\MNC\regent\1eb1be1.jpg" "D:\USER\MNC\"
    

    然后,修改后的列表文件可以保存为例如MoveFiles.bat,使用 ANSI 编码并通过双击执行,或者在命令提示符窗口中查看可能的错误消息,以防文件无法移动由于各种原因。

    但是,这里有一个小批处理文件,用于此任务处理列表文件ListFile.txt。

    @echo off
    if not exist "ListFile.txt" goto :EOF
    
    setlocal EnableExtensions DisableDelayedExpansion
    set "MoveError=0"
    
    for /F "usebackq eol=| delims=" %%L in ("ListFile.txt") do call :ProcessFile "%%L"
    
    if %MoveError% == 1 echo/ & pause
    endlocal
    goto :EOF
    
    :ProcessFile
    set "ListLine=%~1"
    
    rem Remove comma at end of line if there is one at all.
    if "%ListLine:~-1%" == "," set "ListLine=%ListLine:~0,-1%"
    
    rem Replace " to " by a vertical bar being an invalid character in a file
    rem name. The string " to " exists hopefully only once in each line.
    set "ListLine=%ListLine: to =|%"
    
    rem Split up the line into two substrings using | as delimiter.
    rem First string is the file to move, second the target directory.
    rem Execute the file move and output an error message on failure.
    for /F "tokens=1,2 eol=| delims=|" %%I in ("%ListLine%") do (
        move "%%~I" "%%~J" >nul 2>&1
        if not errorlevel 1 goto :EOF
    
        if %MoveError% == 1 echo/
        echo ERROR: Failed to move the file
        echo        "%%~I"
        echo        to "%%~J"
        set "MoveError=1"
    )
    goto :EOF
    

    要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • call /?
    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • if /?
    • move /?
    • rem /?
    • set /?
    • setlocal /?

    另见:

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 2017-02-14
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多