I'm trying to write a batch file that copies folders from one folder location to another, but only if they exist in a third folder.

That sounds confusing, I'll try again.

I have the following 3 folders:

  • Updates
  • Applications
  • Previous Versions

Before updating the \Applications subfolder structure with the [new version] \Updates subfolder structure, I need to copy the \Applications subfolder structure to the \Previous Versions folder. Once that is complete then it needs to copy the Updates subfolder structure to the /Applications folder.

I got as far as the following:

Setlocal EnableDelayedExpansion

Set UpdtRoot=C:\Test\Updates
Set AppsRoot=C:\Test\Apps
Set PVerRoot=C:\Test\Previous Versions

FOR /d %%i IN ("!UpdtRoot!\*.*") DO xcopy "!AppsRoot!\%%~nxi" "!PVerRoot!\%%~nxi\" /e

This doesn't work though, it copies all the folders from AppsRoot into PVerRoot.

I need it to copy subfolders from AppsRoot to PVerRoot, but only if they exist in UpdtRoot. I only need it to copy the folders and subfolders ONLY so no files at all.

  • Forgot to say, it's only folders that I need to copy. – dts256 Jan 28 '16 at 3:53
  • There's no need to keep the whole pre-updated version. I'd just like to be able copy only those folders that are going to be updated. – dts256 Jan 28 '16 at 4:51

I've included a batch script that should complete what you explain to copy all subfolders recursively from the /Applications directory to the /Previous Versions directory, but ONLY if those same directories 'to copy' exist in the /Updates directory.

Notable Items

  • Ensure the SET NewCopyDir=%CopyDir:C:\Test\Apps\=% has the C:\Test\Apps\ characters in it to match the Applications directory full path including the ending backslash. This is the logic that parses that out to append to the end of the /Updates and /Previous Versions directories so the XCOPY commands will copy those directories over accordingly.

  • The Root part off the names were taken off of the SET variables for the directories but that doesn't really matter, but I shorted them up to have it look a little cleaner in the script logic.

Batch Script


SET "Updt=C:\Test\Updates"
SET "Apps=C:\Test\Apps"
SET "PVer=C:\Test\previous Versions"
IF NOT EXIST "%Updt%" MD "%Updt%"
IF NOT EXIST "%Apps%" MD "%Apps%"
IF NOT EXIST "%PVer%" MD "%PVer%"

FOR /D %%S IN ("%Apps%\*") DO (
    CALL :Routine "%%~S"

SET CopyDir=%~1
SET NewCopyDir=%CopyDir:C:\Test\Apps\=%
IF EXIST "%Updt%\%NewCopyDir%" XCOPY /E /T "%Apps%\%NewCopyDir%" "%PVer%\%NewCopyDir%\"

Supporting Resources

  • CALL

      /E           Copies directories and subdirectories, including empty ones.
                   Same as /S /E. May be used to modify /T.
      /T           Creates directory structure, but does not copy files. Does not
                   include empty directories or subdirectories. /T /E includes
                   empty directories and subdirectories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.