There are situations where you want to copy a folder structure without copying the files in that folder and sub-folders. For instance, you may want to replicate the directory structure of your expenses or accounting folders in order to organize them year-wise.
I have the following directory structure to store the documents pertaining to the financial year 2018-19. For the next financial year, I would want to copy the directory structure (without files) to the parent folder named 2019-20 to maintain uniformity.
D:\OFFICIAL\2018-19 ├───Accounts │ ├───Bank Reconciliation Statement │ ├───Capital Gains │ └───TDS ├───Bank Statements │ ├───SB Accounts │ └───Term Deposits ├───Income Tax │ ├───Advanced Tax Receipts │ ├───IT Returns │ ├───Refund Orders │ ├───Self Assessment Tax Receipts │ └───Tax filing acknowledgement
The File Explorer doesn’t allow you to copy the folders without also copying the files. But, there are command-line methods and third-party tools that can easily replicate a directory structure. Let’s discuss the native command-line tools to replicate a folder structure.
Copy folder structure
Important: If your folder names contain spaces, then make sure to enclose the folder path within double-quotes when running one of the commands listed in this article.
Copy folder structure without copying files
Using the XCOPY command
The built-in XCOPY command in Windows can copy the directory or directory tree (i.e., recursively).
Open a Command Prompt window and use the following command-line syntax:
xcopy source destination /t /e
The switches /T /E
make sure that only the folders (including empty folders) are copied without copying the files.
For example:
xcopy "D:\Official\2018-19" "D:\Official\2019-20" /t /e
If the destination folder doesn’t exist, you’ll be asked if the target/destination 2019-20
refers to a file name or directory name (F
= file, D
= directory) on the target. Press D
to continue.
The 2018-19
folder structure is now replicated to 2019-20
.
Note: If your folder names contain spaces, then make sure to enclose the folder path within double quotes when running the command.
/T
Creates directory structure, but does not copy files. Does not include empty directories or subdirectories./T /E
includes empty directories and subdirectories.
Using Robocopy
Using the built-in Robocopy, you can mirror the directory structure (without copying the files) using the following command-line syntax:
robocopy "D:\Official\2018-19" "D:\Official\2019-20" /e /xf *
/e
– Copy subdirectories, including Empty ones./xf *
– Excludes all files — denoted by the wildcard*
Tip: If you want to exclude a particular folder(s), use the /xd
switch.
Using PowerShell
Using PowerShell, you can mirror a directory tree (without copying files) using the following command-line:
Copy-Item -LiteralPath "D:\Official\2018-19" -Destination "D:\Official\2019-20" -Recurse -Filter {PSIsContainer -eq $true}
That’s it. You’ve now replicated the directory structure under another base folder.
Replicate the directory structure on another computer
If you want to replicate the folder structure on another computer that’s not in a network, use one of the above methods in that system to mirror a directory tree.
Then, zip the mirrored folder using File Explorer’s Share tab or via the right-click menu — Send To → Compressed (zipped) folder
option.
Copy the zip file to your computer and extract the contents to a folder. The folder structure will be maintained when you extract it into the target computer.
Alternately, use the batch file method below.
Method 4: Using PowerShell (output a batch file)
Using PowerShell, you can get the list of folders and make a readymade batch file that can create the same directory structure when it’s run from the target computer.
Start PowerShell and use the following command-line syntax:
(gci -Path "folder_path" -Directory -Name -Recurse) | foreach-object {"md `"" + $_ + "`""} | Out-File -FilePath "output_file" -Encoding [type]
Example:
To get the list of directories under the Backup\Accounts
folder recursively and make a batch file, run the following command:
(gci -Path "D:\Backup\Accounts\" -Directory -Name -Recurse) | foreach-object {"md `"" + $_ + "`""} | Out-File -FilePath d:\output.bat -Encoding UTF8
-encoding ASCII
to -encoding UTF8
in the above command-line. Also, check out this SuperUser forum thread Files with non-ASCII characters in the file name in a Windows batch file for the workarounds (chcp
command) you can use when running the batch file.
The output.txt
in the above example is a batch file that creates the exact folder structure recursively.
- Transfer the batch file to the target computer.
- Rename the file with .bat extension — e.g.,
output.bat
- Open a Command Prompt window on the target computer.
- Change to the base directory where you want the batch file to create the subfolders — e.g.,
e:\accounts
.
- Run the file
output.bat
from that directory in the Command Prompt. - Optionally, verify the parent folder’s properties dialog in the source as well as the destination.
That’s it. You’ve copied the folder structure on the target computer using batch file.