代码之家  ›  专栏  ›  技术社区  ›  Shaun_K

是否使用批处理脚本将文件从一个文件夹移动到具有相同层次结构和文件夹名称的另一个文件夹?

  •  0
  • Shaun_K  · 技术社区  · 13 年前

    因此,我希望将所有转换文件夹下的文件从主Source文件夹移动到另一个具有相同层次结构的Output文件夹。

    Source
       -done
       -converted
       -folder1
          -done
          -converted
          -folder11
             -done
             -converted
       -folder2
          -done
          -converted
    

    Output
       (files from converted folder)
       -folder1
          (files from converted folder)
          -folder11
             (files from converted folder)
       -folder2
          (files from converted folder)
    

    我是批处理脚本的新手,所以如果你能在某种程度上帮助我,我将不胜感激。 谢谢

    1 回复  |  直到 13 年前
        1
  •  0
  •   Magoo    13 年前
    @ECHO OFF
    SETLOCAL
    SET source=c:\sourcedir
    SET destroot=c:\destdir
    SET cdtl=0
    :loploop
    SET /a cdtl+=1
    FOR /f "tokens=%cdtl%*delims=\" %%i IN ("%source%") DO IF NOT "%%j"=="" GOTO loploop
    FOR /f "delims=" %%i IN (
        ' dir /b/s/ad "%source%\converted" '
        ) DO CALL :movedest "%%~dpni%"
    )
    GOTO :eof
    
    :movedest
    SET destdir=%~1
    FOR /f "tokens=%cdtl%*delims=\" %%d IN (%1) DO SET destdir=%destroot%\%%e
    ECHO MD "%destdir%" 2>NUL
    ECHO MOVE /y "%~1\*.*" "%destdir%\"
    GOTO :eof
    

    这个 ECHO 倒数第二行中的s简单地显示了建议的操作。在验证了您想要做的事情之后,只需删除 回声 关键字执行移动。

    请注意 /y 切换到 MOVE 命令将覆盖文件的任何现有目标版本。