1

I need to recursively copy a set of files using a batch (DOS) script, maintaining the original directory structure. Sounds easy, right? Here are the complications:

  1. xcopy is not available on the server I will be using the batch script on. Therefore, I have to use the copy command.
  2. A thorough Internet search of this topic only leads to the use of xcopy, or some specialized use of the script (this is just a generic copy/paste job).
  3. I have not written in DOS since the 90's.

How do I get the copy command to save to a new directory with the same name/location as the old one?

  • *robocopy* ships with some versions of Windows and is like xcopy on steroids. – Alex K. Jan 09 '14 at 16:42
  • 2
    What version of Windows are you using that does not have XCOPY? I thought all versions of Windows had XCOPY. Are you sure that your PATH is set appropriately to include the system folder where external commands reside? – dbenham Jan 09 '14 at 17:03
  • Server 2008, but it's been really stripped down and probably misconfigured. Just another challenge heaped on my already full plate! – user3178452 Jan 09 '14 at 17:37
  • I suggest you to see: [recursively-find-and-replace-files](http://stackoverflow.com/questions/10422433/recursively-find-and-replace-files/10423757#10423757) – Aacini Jan 09 '14 at 18:40
  • Is there a reason why you can't copy `xcopy.exe` from a normal server? – foxidrive Jan 10 '14 at 04:23

2 Answers2

1

This is untested. The code supposedly duplicate the folder structure, but not copy files. If the test seems to be correct, remove the ECHO part from the copy command. The first parameter is "sourceDir" and the second one is "targetDir".

EDIT: Small detail fixed

@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 ..
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
0
@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%
MC ND
  • 69,615
  • 8
  • 84
  • 126