How to Copy a Folder in Windows 10
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 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
.
-
/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
PowerShell outputs to an ASCII file by default. It would be better to choose UTF8, especially if you have foreign characters in the folder names. In that case, replace -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.
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
So thank you so much for your support, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)
How to Copy a Folder in Windows 10
Source: https://www.winhelponline.com/blog/how-to-copy-folder-structure-without-copying-files/
0 Response to "How to Copy a Folder in Windows 10"
Post a Comment