Friday, January 29, 2010

Playing a Concert on a Kazoo: Collecting Files in DOS

Wouldn’t it be nice to apply a script of any complexity to a whole lot of files all at once?  It’s possible if you’ve got a list of addresses for each of those files.  How do you get a list of addresses?  I use the DIR command in DOS.

You're going to need a bit of background in DOS if you don't have it already**.  Here it goes...

If you are familiar with AutoCAD then you are familiar with the Command line.  It is that constant flow of text across the bottom of the AutoCAD window.  That piece of the interface is EXTREMELY old.  All computer programs through the 60s, 70s and into the 80s ran from a command line.  In fact Windows is called “Windows” because it broke away from the command line into a window based interface.  Before then the Microsoft operating system was called DOS and was entirely text based (at least that’s what users worked with).

There are still vestiges of the old system hanging around.  If you click on the start button and select Run… and type cmd you’ll get a command line window that allows you to access the operating system.  You can navigate up and down through folders as you do in the Graphical User Interface (GUI).   You can copy files, and do all the other things you’d do through the GUI, it just looks different.



There are things you can do with DOS, that you can’t do in Windows (at least not without a special utility).  In particular, you can create a list of files from a directory.  There’s a lot of other stuff you can do, but that gets deeper into DO than I’d like, so I’m going to leave that between you and the help files (HINT: Start Button: Help and Support).  I’ll just give you an example of what I use to collect the addresses I need:

dir "C:\Documents and Settings\mritzman\My Documents\ACAD_Excel\*.dwg" /s /b > drawing_list.txt

This command line instruction will scan through the given location and collect all the DWG file names (and paths) into a text file named drawing_list.txt.  The text file output is placed in the current folder as indicated by the command line.


Now that we’ve got the list of files, we need to open them, run a series of commands, save and close the files.  The basic template is this:

OPEN “C:\...Path…\dwg_name(1).dwg” COMMANDS QSAVE CLOSE
OPEN “C:\...Path…\dwg_name(2).dwg” COMMANDS QSAVE CLOSE
And so forth. 

Note that we need quotation marks around the file path and name.  (This is because we may have spaces that would normally cause AutoCAD to start a new command). 

Create this as a template in Excel.


  • For our purposes, we’ll reuse the function we started with:  add a square to the origin of each of each file.
  • Fill in the columns from our sources.  
  • Add in the concatenate command.  
  • Make sure quotations and spaces are in the right places.   
    • Add a space and quotes at the end of the OPEN command 
    • Add quotes and a space at the beginning of the command series (REC and so forth)
    • Add a space after the QSAVE command
  • Add an =CONCATENATE() series to tie everything together.
It should look something like this:



AutoFILL throughout the rest of the spreadsheet.

Copy this to Notepad and run it as a script in AutoCAD.

Once again, here’s a spreadsheet as a Google Doc.  Unfortunately, the paths and names are unique to my computer, so you'll have to go to some extra effort to get all the pieces to come together properly.

In this section, we’ve covered two main points:
1.    How to collect file information from the DOS prompt.
2.    How to deal with spaces in file paths and names. (Use quotes.)

In the next post well pull information from AutoCAD.  We’ll run into more situations where syntax between Excel and AutoCAD doesn’t translate directly.  And we'll figure out how to correct those problems.

** I used to take the command line for granted.  Then I found this essay by Neil Stephenson: In the Beginning was the Command Line It is beautifully written and well worth the effort to download.

No comments:

Post a Comment