In this tutorial, we’ll cover a quick-and-dirty way using the command line to Mirror Folder Structure using Batch in Windows, including sub-folders.

As with all Windows Batch files, steer clear of the top level C:\ drive. Not only is this a “special” location in Windows, there is no higher level of folders.

This plays a big part in these mirror/copy/move scripts, as you cannot move outside of the C:\ drive to a higher level folder.

Let’s go through a quick example;

Say we want to Mirror the folder structure of C:\Temp.
This folder is usually empty, but for this example, I’ve made 2 folders, “123” and “ABC”.

Their structure looks like this;

C:\Temp\ABC\123

If we were to run the “mirror folders” batch script from directly within C:\Temp, by design, this script is going to try and mirror them back out of the Temp folder, into the root level C:\ drive, but safely inside their own directory “_MIRROR”.

This may cause issues if you don’t have sufficient user permissions, and even by “Running as Admin” because the script directly attempts to create folders on the root level C:\ drive, errors may occur.

The simplest way around this is to “push” the levels of folders up by 1.

So instead of C:\Temp\ABC\123 we could create C:\Temp\PUSH\ABC\123 and then move our mirror folder batch script into the PUSH folder itself.

This way, when we run the script, it will create the _MIRROR folder inside C:\Temp which we should have full access to 🙂

As with all batch scripts, we should start with

@ECHO OFF
@SETLOCAL enableextensions
@CD /d "%~dp0"

Next, we will SET some variables;

SET TYP=DIR_MIRROR
SET TTL=Copy Utility [%TYP%]
SET DIR=..\_%TYP%
SET CPR=©2008-2016 Jenkins Media. All Rights Reserved.
TITLE %TTL%

We must make sure our target directory exists first;

REM Make the Target directory
MKDIR "%DIR%"

Now, we can do our folder mirroring;

REM Do the (X)copy
REM MUST USE %CD%
REM DO NOT USE %1 OR %~dp0
XCOPY "%CD%" "%DIR%" /T /E

The /T and /E switches of XCOPY tell CMD that we want to only use folders, and include Empty ones too

This one is optional. We can move the target directory back to where we ran the script from if needed;

REM Move Target dir back to Source dir
MOVE "%DIR%" "%~dp0"

Finally, print some helpful messages to the console, halt the output (press any key to continue) and then safely Exit.

ECHO ------------------------
ECHO %TTL% %CPR%
ECHO ------------------------
ECHO Grab Completed at %TIME% - %DATE%
PAUSE
EXIT

That’s it!

@ECHO OFF

@SETLOCAL enableextensions
@CD /d "%~dp0"

SET TYP=DIR_MIRROR
SET TTL=Copy Utility [%TYP%]
SET DIR=..\_%TYP%
SET CPR=©2008-2016 Jenkins Media. All Rights Reserved.
TITLE %TTL%

REM Make the Target directory
MKDIR "%DIR%"

REM Do the (X)copy
REM MUST USE %CD%
REM DO NOT USE %1 OR %~dp0
XCOPY "%CD%" "%DIR%" /T /E

REM Move Target dir back to Source dir
MOVE "%DIR%" "%~dp0"

ECHO ------------------------
ECHO %TTL% %CPR%
ECHO ------------------------
ECHO Grab Completed at %TIME% - %DATE%
PAUSE
EXIT

Report This Post