AfraLISP - Learn AutoLISP for AutoCAD productivity

The DIR Command

by Kenny Ramage

Important note: Before proceeding with this tutorial, ensure that the following is set correctly within your Acad.pgp file (AutoCAD Program Parameters File.)

DIR, DIR, 1,File specification: ,

You will find the "DIR" parameters  under the "Examples of external commands for command windows" section within your Acad.pgp file.

The Acad.pgp file can normally be found within your AutoCAD Support directory, for example: C:/Program Files/Autocad 2002/Support/Acad.pgp. You can simply open and edit this file using Notepad.

The "DIR" command is a DOS (remember that dark place) command that displays a list of a directories files and subdirectories. But did you know that you can use the "DIR" command from within AutoLisp? This is a great function for obtaining a list of files in a directory, especially useful for Batch Processing Routines.

Let's have a look at it. Fire up AutoCAD and type this at the Command prompt :

	Command: (command "dir" "*.* > Temp.txt")

What this statement is asking for is a directory listing of all the files in the current directory output to the file Temp.txt.

Now open Temp.txt and you should have something like this :

 Volume in drive O has no label
 Directory of O:\E51D\E51D1
 
.           <DIR>     09-11-99  3:13p .
..          <DIR>     09-11-99  3:13p ..
9961     DXF       345,167  08-05-99 12:14p 9961.DXF
9962     DXF     8,246,298  08-05-99 12:14p 9962.DXF
Ant      exe       701,121  08-05-99 11:36a Ant.exe
batchp1  exe        11,776  01-18-99 11:23a batchp1.exe
BATCHP1 EXE        13,312   01-19-99  8:19a BatchPurge.Exe
DCLTUT   GIF         2,635  08-01-99 11:19p DCLTUT.GIF
debex       <DIR>     07-28-99  7:04a debex
N1       dxf       245,702  08-02-99  3:23p N1.dxf
NAMDEB   DXF    28,320,177  07-30-99 12:29p NAMDEB.DXF
projair  dwg       120,055  07-06-99  3:11p projair.dwg
R12K471 DWG       421,118   07-02-99 10:28a R12K47646.dwg
sort     dwg       125,471  07-26-99  1:17p sort.dwg
SORTER   dwg        27,981  07-27-99 10:35a SORTER.dwg
temp        <DIR>     09-11-99  3:13p temp
temp     txt             0  09-11-99  3:13p temp.txt
test       <DIR>      08-20-99 11:15a test
title    dwg        99,381  08-02-99  2:30p title.dwg
truss1   dwg        84,382  07-09-99 11:13a truss1.dwg
truss2   dwg        88,296  07-09-99 11:13a truss2.dwg
uniglide dwg       205,715  07-19-99  9:09a uniglide.dwg
VALVE    dwg        24,693  07-29-99 11:31a VALVE.dwg
        18 file(s)     39,083,280 bytes
         5 dir(s)   2,147,450,880 bytes free

This is a listing of all the files and sub directories contained in your current working directory. OK, I know, it's not quite formatted the way we would want it to be. So, let's use a couple "DIR" switches to format the file list the way that we would like it. Try this :

	Command: (command "dir" " /b *.* >temp.txt")

That's better, we've now got just the file names. The /b switch limits the list to only file names. Temp.txt should now look something like this :

9961.DXF
9962.DXF
Ant.exe
batchp1.exe
BatchPurge.Exe
DCLTUT.GIF
debex
N1.dxf
NAMDEB.DXF
projair.dwg
R12K47646.dwg
sort.dwg
SORTER.dwg
temp
temp.txt
test
title.dwg
truss1.dwg
truss2.dwg
uniglide.dwg
VALVE.dwg

Right, now retrieve just the DWG filenames :

	Command: (command "dir" " /b *.dwg > temp.txt")

Temp.txt should now look something like this :

projair.dwg
R12K47646.dwg
sort.dwg
SORTER.dwg
title.dwg
truss1.dwg
truss2.dwg
uniglide.dwg
VALVE.dwg

That's it, we have now got our required file listing in the format that we require. The "DIR" command has got a lot more switches that you can use to restrict your listings to whatever you desire. I suggest you dust off one of your old DOS Reference Manuals and look "DIR" up.

Hint : Would you like a listing of all the files in the current directory AND all sub-directories? Use the /s switch :

	Command: (command "dir" " /b/s *.dwg >temp.txt")

You should get a listing something like this :

O:\E51D\E51D1\projair.dwg
O:\E51D\E51D1\R12K47646.dwg
O:\E51D\E51D1\sort.dwg
O:\E51D\E51D1\SORTER.dwg
O:\E51D\E51D1\title.dwg
O:\E51D\E51D1\truss1.dwg
O:\E51D\E51D1\truss2.dwg
O:\E51D\E51D1\uniglide.dwg
O:\E51D\E51D1\VALVE.dwg
O:\E51D\E51D1\test\SCANNEX1.dwg
O:\E51D\E51D1\test\SCANNEX2.dwg
O:\E51D\E51D1\test\SCANNEX3.dwg
O:\E51D\E51D1\test\SCANNEX4.dwg