Given an existing directory containing files and folders, how can I reproduce the same structure somewhere else on the hard drive (or otherwise), but instead of copying the full file contents, just have dummy files with the same name?
This question came from a question on Atomic MPC forums that I thought would be really simple on unix given the multitude of shell utilities, but might be a little tricky on Windows command prompt.
So first, we want to re-create present working directory structure, replacing “C:\test” with where you want the files to be placed:
for /f "delims=" %i in ('dir /a:d /b /s') do @mkdir "C:\test%~pnxi"
Then, we’ll just write empty files for every name that we have in our current directory into the target directory.
for /f "delims=" %i in ('dir /a:-d /b /s') do @echo. > "C:\test%~pnxi"
So it’s not that difficult after all. Note that this won’t copy hidden files across – if you know you have them, then you probably know how to tweak the command to get them across too.
Enjoy.
Update: Here’s an explanation of how it works:
Step 1
Let’s start with the ‘dir’ part, that is used to give me a directory listing.
‘/a:d’ gives me only directories, and not files.
‘/b’ gives me the result in bare formatting, so only the path and the name of the directory.
‘s’ includes all subdirectories.
So, having done this, I now have a list of all current subdirectories. Now for every item in this list, (the ‘for’ command), I will set the variable ‘%i’ to that directory, and call the ‘mkdir’ command. The ‘delims=’ part makes sure the directory isn’t split by spaces.
Finally, I ‘mkdir’ the new path, with is composed of C:\test at the beginning, and then append a special formatting of the directory path. %i would just give me the bare string, but I can extract the path ‘p’, the name ‘n’ and extension ‘x’ in that order, giving me %~pnxi. The reason for doing this is to remove the drive letter at the start so I can put it into the C:\test subdirectory.
The @ annotation before the command just suppresses printing of the command, so I don’t see anything if everything goes well.
Step 2
The for loop here is similar, but instead of looping over the directories, I’m not looping over non-directories. ‘/a:-d’.
The command here is ‘echo.’ which prints a newline character (I think), and sends that to a file formatted in a way similar to step 1.
Update: It has been pointed out that ‘robocopy’ has a ‘/create’ flag that handles this as well. Thanks!