【问题标题】:Recursively Copy Files with Batch Script Without Using Xcopy在不使用 Xcopy 的情况下使用批处理脚本递归复制文件
【发布时间】:2014-01-09 16:39:02
【问题描述】:

我需要使用批处理 (DOS) 脚本递归地复制一组文件,同时保持原始目录结构。听起来很容易,对吧?以下是并发症:

  1. xcopy 在我将使用批处理脚本的服务器上不可用。因此,我必须使用复制命令。
  2. 对该主题进行彻底的 Internet 搜索只会导致使用 xcopy 或脚本的某些特殊用途(这只是一般的复制/粘贴工作)。
  3. 自 90 年代以来我就没有在 DOS 中编写过。

如何让复制命令保存到与旧目录同名/位置的新目录?

【问题讨论】:

  • robocopy 随 Windows 的某些版本一起提供,就像 xcopy 一样。
  • 您使用的是哪个版本的 Windows 没有 XCOPY?我认为所有版本的 Windows 都有 XCOPY。您确定您的 PATH 已正确设置为包含外部命令所在的系统文件夹吗?
  • Server 2008,但它已经被真正剥离并且可能配置错误。又一个挑战堆在我已经吃饱的盘子上!
  • 建议大家看看:recursively-find-and-replace-files
  • 不能从普通服务器复制xcopy.exe有什么原因吗?

标签: windows batch-file recursion


【解决方案1】:

这是未经测试的。该代码应该复制文件夹结构,但不复制文件。如果测试似乎正确,请从copy 命令中删除ECHO 部分。第一个参数是“sourceDir”,第二个参数是“targetDir”。

编辑:修复小细节

@echo off
if not exist %2 md %2
set targetDir=%~F2
cd %1
call :processFolder
goto :EOF

:processFolder
setlocal EnableDelayedExpansion
rem For each folder in this level
for /D %%a in (*) do (
   rem Enter into it, process it and go back to original
   cd %%a
   set "targetDir=%targetDir%\%%a"
   if not exist "!targetDir!" md "!targetDir!"
   ECHO copy *.* "!targetDir!"
   call :processFolder
   cd ..
)

【讨论】:

    【解决方案2】:
    @echo off
    
        setlocal enableextensions disabledelayedexpansion
    
        set "exitCode=0"
    
        set "sourceDir=%~1"
        set "targetDir=%~2"
    
        if not defined sourceDir (
            call :usage
            goto endProcess
        )
        if not defined targetDir (
            call :usage
            goto endProcess
        )
    
        for %%f in ("%sourceDir%") do set "sourceDir=%%~ff"
        for %%f in ("%targetDir%") do set "targetDir=%%~ff"
    
        if not exist "%sourceDir%" (
            call :error "Source directory does not exist"
            goto endProcess
        )
        if /i "%sourceDir%"=="%targetDir%" (
            call :error "Source and target are the same"
            goto endProcess
        )
    
        ver > nul
        call :recursiveFileCopy "%sourceDir%" "%targetDir%"
        if errorlevel 1 set "exitCode=1"
    
        goto endProcess
    
    :recursiveFileCopy sourceDir targetDir  
        setlocal
        set "sourceDir=%~f1"
        set "targetDir=%~f2"
        if not exist "%targetDir%\" md "%targetDir%" || call :error "Failed to create [%targetDir%]"
        if not errorlevel 1 (
            dir /a-d "%sourcedir%\*" >nul 2>nul && copy /y "%sourcedir%\*" "%targetdir%"
            pushd "%sourcedir%"
            for /d %%d in (*) do if not errorlevel 1 call :recursiveFileCopy "%%~fd" "%targetDir%\%%~nxd"
            popd
        )
        endlocal
        goto :eof
    
    :usage
        echo(
        echo( Usage: %~n0 sourceDir targetDir
        echo(
    :error
        echo(%~1
        set "exitCode=1" & cmd /d /q /c exit /b 1
        goto :eof
    
    :endProcess
        endlocal & exit /b %exitCode%
    

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 2023-02-22
      • 2015-02-01
      • 2022-01-13
      • 1970-01-01
      • 2012-03-27
      • 2016-03-30
      • 1970-01-01
      • 2012-10-30
      相关资源
      最近更新 更多