Mirror folder structure (flat, single tree level only)
December 27th, 2007
(Requires Win2k, XP, or Vista) Using FOR at the command line to step through every subfolder in a given folder, and create new folders with those names in a different parent folder.
Suppose you have a group of folders under C:\test1, like this:
C:\ | +-test1 | +-folder1 | +-folder2 | +-folder3
And you want to create a new test2 folder under C:\, which will contain emtpy folders with the same names as those in test1, like this:
C:\ | +-test2 | +-folder1 | +-folder2 | +-folder3
Navigate to your target folder (in this case, C:\test2), and enter the following command:
for /d %a in ("..\test1\*") do md "%~na"
The /d tells it to only look at folder names, and the %a is the placeholder variable for each found item. Note that the second reference to %a has a ~n inserted to tell it to only reference the name of the found item - otherwise the entire path will be used, and it will attempt to create new folders on top of the existing ones in the source location.
Note that you can also clear out any existing folders in the source that are empty (if those are no longer needed in the new mirror) by using the following command from the source folder:
for /d %a in (*) do rd /q "%a"
Note also that you must use double percents (%%) if you include those commands in a batch file, to differentiate from batch file variables.
Leave a Reply