Skip to content
October 25, 2006 / Steven Pousty

quick little DOS trick

Ed walks into my office and asks if there is a way to get a listing of the files in a directory so he can put it into a spreadsheet. Seeing as how Windows is targeted to the GUI I suggest a quick print screen. He then explains that he wants the listing as data, not as a printout on the screen. I know telling Ed to download cygwin is not going to cut it so I start to tell him there is no hope. But then all the scripting I had to do to admin several Windows labs come back to me and I work out doing this:

dir /OG > listing.txt 

which sorts the directories first and then the files and puts it into a text file, listing.txt. The contents of this file can then be placed in excel and quickly parsed using text to column. I know there is probably some sort of perl script or something I could write to do this (I can already see the Java code in my head) but this was beyond Ed and I didn’t have the time to write it for him. So there you have a simple way to turn a directory listing into data. I am sure most of you knew this but it serves as a good reminder to myself.



Leave a Comment
  1. Brian Timoney / Oct 25 2006 11:20 am


    In the spirit of “if it’s stupid, and it works, then it ain’t stupid”, I give you a thumbs up. And I think there is a natural age cut-off (30? 35?) where assuming any type of DOS knowledge might be misplaced.

    Is it too much to hope that Vista makes this just as easy?

    Anyway, adding the /s switch gives you listings for all your subdirectories as well.

    >dir /s /og >listings.txt

    Note to bloggers–when there’s nothing to rant about, mini-hacks and shortcuts are always appreciated!


  2. Brian Flood / Oct 25 2006 12:10 pm

    MONAD rocks, its part of Vista but is supported in XP and 2003 Server.


  3. Jeremy / Oct 25 2006 1:05 pm

    If you just want the file names use the bare command.

    >dir /b /og > listings.txt

    dir /? is your friend!

  4. Walt / Oct 25 2006 2:43 pm

    Jeremy beat me to it, but he’s spot on. The /b switch makes a directory listing to file much cleaner.

    I use dir /b… for quite a lot of batch scripts.

  5. thesteve0 / Oct 25 2006 10:47 pm

    You guys rock! Yeah I saw all those options with the /? but he wanted it quick. The funny part is that I had to send him instructions on how to do the command since it was beyond him. So I think Brian F’s suggestion would have been over the top. How funny would the discussion have been if I said: Ok first go install Monad, the you need to learn a bit of C# scripting language, oh don’t worry it is object oriented so instead of needing to learn awk to munge the text you can ask the object for it to list itself. Watch as he throws up on my desk….

    Actually Ed wanted the dates of the file. The /s is handy since he might want that but I think this was enough to get him started….

  6. D / Oct 25 2006 11:00 pm

    what about tree? e.g. C:\some_dir\tree > file_struct.txt

  7. thesteve0 / Oct 25 2006 11:07 pm

    Hey D:
    Doesn’t work well when output to text file but holy canoli that is a cool command. Where did that come from? Who snuck that into my dos when I wasn’t looking…

  8. pierre / Oct 26 2006 3:06 am

    Hey, i am 22 and have known these switches as well as > …

    I am not a unix/linux guy, these folks can do much more interesting things than that, I guess 🙂

  9. D / Oct 26 2006 7:42 am

    Hey Steve,
    Yeah, not sure when I stumbled on tree but it is a pretty cool command. You’re right, it’s not all that useful…I still haven’t found a good way to deal with the output as a .txt file other than opening it in word as MS-DOS text (this will make the text look like what you see in the cmd window). Opening in a vanilla text editor gives you some unreadable garbage.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: