1

I need to create a folder structure via command line, structure is as follows :

username > folder1 > folder2 > folder3 (and there are subdirectories within these folders as well)

This is a windows host, I have done "dir /ad > dir.txt (It outputs the parent folder)

Also, which command on dos would I use to create folder from text file ?

I have used xcopy /T /E (it fails at a certain point as the files are being read from these folders majority of the time), I have around 1500 folders and then sub-folders within these folders.

2
  • does the xcopy error out with a access denied etc?
    – tony roth
    Mar 19, 2012 at 19:55
  • no - it says file folder corrupted - not access denied - thanks for your comment.
    – Mutahir
    Mar 19, 2012 at 20:03

2 Answers 2

2

It's not realy clear what you want. Do you want to replicate a directory structure from one PC to the other and is there a connection between the host on which you created the textfile? If so, you can copy a directory structure without the files in it using the commandline:

robocopy [source] [destiantion] /Create

Or is it that you have a textfile with all the paths in it that you want to create. If so you might want to be creative with notepad. For example if you have a file that has the directories you want to create like:

c:\Users\john\dir1

Open the file with notepad. Press CTRL+H and replace 'C:' with 'MD C:'. Your file should look like this.

MD c:\Users\john\dir1

Now save the file as a batchfile (e.g. createfolder.cmd) and doubleclick it. It will create all the folders. If some directories contain spaces this will fail. You can paste all in excel in colmn B. In colmn A, add MD ", in colmn C add " select all and copy to notepad again. Now replace all Tab's with an empty string and save as a batchfile.

I hope it helps.

PS: If you are familiair withe to 'for' command this would be easier, but 'for' is indeed rather complicated.

6
  • I have a folder structure (a standard) where we have a user name and then 8 folders within that username, there can be numerous folders within those sub-folders too. I have the folder structure on a source at the moment but i has around 8TB of Data inside it (text files, indesign files, ps files) what I want to do is to basically create an exactly same DIRECTORY structure without actually copying the content of those folders. I tried your robocopy command - it copied some files with 0KB but no directories - will be most grateful for your help !! Thanks for your help
    – Mutahir
    Mar 19, 2012 at 20:18
  • 1
    robocopy [source] [destination] /Create /E will go into all of the subdirectories. You can also provide a condition that you know none of the files satisfy to avoid copying them, for instance robocopy [source] [destination] /Create /E /maxage:1
    – JamesCW
    Mar 19, 2012 at 20:23
  • Indeed I forgot /E. The syntax of JamesCW should do the trick.
    – ZEDA-NL
    Mar 19, 2012 at 20:31
  • would probably use powershell to create this instead of cmd's "for" command.
    – tony roth
    Mar 19, 2012 at 21:04
  • 1
    I always use /mir. This basicaly replicates folders (copies all, bus also removes extra files). If you also want the file permissions to be copied, use /mir /sec. If you want to throttle the bandwidth, so you can copy in business hours without anyone noticing you can add /IPG:50 (it slows down the copy though). The best thing about robocopy /mir while migrating is that you can copy whenever you like. On going Live you run Robocopy /Mir once more and it will copy (and delete) only the differences in no time.
    – ZEDA-NL
    Mar 20, 2012 at 9:02
3

You're question is not very clear about what you actually need to do.

As I read it you have list in a text file consisting of all the folders you need to create which was created by doing a dir /ad.

That is you first problem. If you do this in the users home-folder on Vista or Win7 you get a mix of dirs and junctions. You will have decide how to threat those. (Maybe skip the junctions ?)

Then you can use a variant on the FOR command to read the text file and call the MKDIR command for each folder.

Use FOR /? and MKDIR /? for some help. You will need CMD Extensions enabled. See CMD /? on how to do that if neccessary.

Be warned: FOR has very obscure and cryptic syntax, but it can do some amazing things if used properly.

EDIT: After seeing the comments (and having a Windows box handy to check syntax):

dir /ad /b /s will get you a plain folder-list.

You will have to edit the output in a text-editor to change the drive-letter at the from or insert some extra level of folders some-where. You have to place a " before and after each line to handles paths with spaces properly.

Then run it through FOR like this:

FOR /F "usebackq"  %i IN (dir.txt) DO MKDIR %i

Please note: The FOR command requires you to use %%i in stead of %i when you use it in a batch-file (as opposed to interactively).

3
  • curious to see if he's trying to migrate users home dirs etc from one server to the next.
    – tony roth
    Mar 19, 2012 at 19:57
  • Hi Tonny, dir /ad > output isn't helpful as it lists date as well, can you please provide me with a command which will output an existing structure to a text / csv file and then a command which will allow me to re-create it on as many servers as I want to, I have around 8000 folders in all but one directory have around 1200 folders and then subfolders within them - will be most grateful
    – Mutahir
    Mar 19, 2012 at 20:05
  • See my edits in the answer
    – Tonny
    Mar 19, 2012 at 21:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .