Command-Line folder structure on Windows

18
2014-04
  • rihatum

    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.

  • Answers
  • ZEDA-NL

    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.

  • Tonny

    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).


  • Related Question

    Useful Command-line Commands on Windows
  • Questioner

    The aim for this Wiki is to promote using a command to open up commonly used applications without having to go through many mouse clicks - thus saving time on monitoring and troubleshooting Windows machines.

    Answer entries need to specify

    • Application name
    • Commands
    • Screenshot (Optional)

    Shortcut to commands


  • Related Answers
  • Some image demonstrationg the use of mstsc.exe. Sung

    A little known one is

    getmac
    

    It shows the MAC address(es) of your network adapter(s).

    Screenshot of running getmac from a Windows commandline window.

  • enter image description here hmemcpy

    A list I use a lot:

    • nbtstat - List NetBIOS stats and information
    • netstat - List TCP/IP stats and information
    • ipconfig - List TCP/IP configuration for a system
    • netsh - Network configuration for a system
    • sc - manage services
    • net - whole slew of commands to manage users and groups, shares, connections, etc.
    • ping - makes sure a system is up on the network
    • tracert - trace the hops between two hosts. useful to see if there's a break in between and where it is.
    • nslookup - Query DNS for information
    • dcdiag - check health of the domain controller
    • setspn - check SPNs for Kerberos configuration
  • alt text Sung

    I find that I use findstr a lot to find stuff in logs, error files, etc.

    A simple example: in the log file ex0905.log we find all lines that have 2009-05-05 in them:

    findstr "2009-05-05" ex0905.log
    
  • enter image description here Sung

    Sometimes I have to worry about too few free sessions for a Terminal Server connection to a server.

    quser displays information about user sessions on a terminal server.

    quser /SERVER:myserver
    

    Output

    C:\Documents and Settings\sysmanager01>quser /SERVER:serverx
    USERNAME                    SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
    usr_hot1                                        1  Disc        none   30.04.2009 17:59
    usr_hot                                         2  Disc        none   30.04.2009 18:01
    appsuperuser                rdp-tcp#6           3  Conn            .  01.01.1601 02:00
    

    Sometimes it's even possible to find pure workaholics like appsuperuser :-)

    It's easier when pasting the quser executable from any 32-bit Server to my local System32 folder.

  • enter image description here Sung

    Very useful one I only found out about recently:

    winver.exe
    

    Gives you a dialog box with the version of Windows the machine is running, complete with Service Pack level and build number.

  • Area 51

    Registry Editor

    regedit