Visual Lisp - Directories
and Files - Page I
Using plain old AutoLisp, we really only
have two functions dealing with files and directories namely, the
(findfile) function, and the (getfiled) function. Both of these functions
are useful but limited in there scope.
Visual Lisp has introduced a whole host of new functions specifically
designed to use with files and directories. In this tutorial we're going
to have a look at a few of them. Firstly, we'll have a look at probably
the most powerful Visual Lisp function for dealing with files and
directories, the (vl-directory-files) function. Fire up AutoCAD, open the
Visual Lisp Editor, then type this at the console prompt :
_$ (vl-load-com)
_$ (vl-directory-files)
("." ".." "X3426.dwg"
"addshort.dvb" "ADMENU.DVB"
"afralispURL.dvb" "afraLOGO.dwg"
"another-dump.lsp" "area.zip"
"arrowkeyview.dvb" "ATTAB.DCL" "ATTAB.DWG"
"ATTAB.LSP")
This will return a list containing every file and sub-directory within
your current directory.
Let's have a look at the syntax of (vl-directory-files) and a few other
Visual Lisp File/Directory functions.
VL-DIRECTORY-FILES
Lists all files in a given directory
(vl-directory-files [directory
pattern directories])
Arguments :
directory
- A string naming the directory to
collect files for; if nil or absent, vl-directory-files uses the
current directory.
pattern
- A string containing a DOS pattern for
the file name; if nil or absent, vl-directory-files assumes
"*.*"
directories
- An integer that indicates whether the
returned list should include directory names. Specify one of the
following :
-1 List
directories only.
0 List
files and directories (the default).
1 List files
only.
Return Values
- A list of file and path names, or
nil, if no files match the specified pattern.
Let's try it again, but this time with
some arguments. Type this at the console prompt :
_$ (vl-directory-files
"d:/drawings" "*.dwg")
("X3426.dwg" "afraLOGO.dwg"
"ATTAB.DWG" "Drawing1.dwg" "Drawing4.dwg"
"is handsome.dwg" "tem5.dwg" "X3374.dwg"
"X3375.dwg" "dwgdata.dwg" "Drawing2.dwg"
"X3359.dwg" "tblock.dwg" "kenny.dwg"
"Adaptor.dwg" "Drg-1.dwg" "drg-2.dwg"
"attab-vl.dwg" "matlist.dwg")
This will return a list of all drawings residing in the directory
"d:/drawings".
Let's try something else :
_$ (vl-directory-files
"d:/drawings" "*.lsp")
("another-dump.lsp" "ATTAB.LSP"
"BAREA.LSP" "bick.lsp" "acad.lsp"
"mted.lsp" "circle-react.lsp" "clay.lsp"
"DC-Delete.lsp" "endPlot.lsp")
This, of course, will return a list of all AutoLisp files.
Now let's get clever. Let's try and get a list of just the subdirectories
:
_$ (vl-directory-files
"d:/drawings1" nil -1)
("." ".."
"Purge-Export" "Office" "3D"
"Extext" "VBA" "DrawingExfiles")
Easy hey. Now
let's have a look at some of the other Visual Lisp file handling
functions.
VL-FILE-COPY
Copies or appends the contents of one
file to another file
(vl-file-copy
source-file destination-file [append])
Copy or append the contents of one file to another file. The vl-file-copy
function will not overwrite an existing file, only append to it.
Arguments :
source-file
- A string naming
the file to be copied. If you do not specify a full path name,
vl-file-copy looks in the AutoCAD start-up directory.
destination-file
- A string naming
the destination file. If you do not specify a path name, vl-file-copy
writes to the AutoCAD start-up directory.
append
- If specified
and not nil, source-file is appended to destination-file (that is,
copied to the end of the destination file).
Return Values
- An integer, if
the copy was successful, otherwise nil.
Some typical reasons for returning nil are:
source-file is not readable
source-file is a directory
append? is absent or nil and destination-file exists
destination-file cannot be opened for output (that is, it is an
illegal file name or a write-protected file)
source-file is the same as destination-file
Examples
Copy autoexec.bat to newauto.bat:
_$ (vl-file-copy "c:/autoexec.bat"
"c:/newauto.bat")
1417
Copy test.bat to newauto.bat:
_$ (vl-file-copy "c:/test.bat" "c:/newauto.bat")
nil
The copy fails because newauto.bat already exists, and the append argument
was not specified.
Repeat the previous command, but specify append:
_$ (vl-file-copy "c:/test.bat" "c:/newauto.bat" T)
185
The copy is successful because T was specified for the append argument.
VL-FILE-DELETE
Deletes a file
(vl-file-delete
filename)
Arguments :
filename
- A string containing
the name of the file to be deleted. If you do not specify a full path
name, vl-file-delete searches the AutoCAD start-up directory.
Return Values
- T, if successful, nil
if delete failed.
Examples
Delete newauto.bat:
_$ (vl-file-delete "newauto.bat")
nil
Nothing was deleted because there is no newauto.bat file in the AutoCAD
start-up directory.
Delete the newauto.bat file in the c:\ directory:
_$ (vl-file-delete "c:/newauto.bat")
T
The delete was successful because the full path name identified an
existing file.
VL-FILE-DIRECTORY-P
Determines if a file name refers to a
directory
(vl-file-directory-p
filename)
Arguments :
filename
- A string containing a
file name. If you do not specify a full path name, vl-file-directory-p
searches only the AutoCAD start-up directory.
Return Values
- T, if filename is the
name of a directory, nil if it is not.
Examples
_$ (vl-file-directory-p "sample")
T
_$ (vl-file-directory-p "yinyang")
nil
_$ (vl-file-directory-p "c:/program files/acad2000")
T
_$ (vl-file-directory-p "c:/program
files/acad2000/vlisp/yinyang.lsp")
nil
VL-FILE-RENAME
Renames a file
(vl-file-rename old-filename
new-filename)
Arguments :
old-filename
- A string containing the name of the
file you want to rename. If you do not specify a full path name,
vl-file-rename looks in the AutoCAD start-up directory.
new-filename
- A string containing the new name to
be assigned to the file.
NOTE If you do not specify a path name, vl-file-rename writes the
renamed file to the AutoCAD start-up directory.
Return Values
- T, if renaming completed
successfully, nil if renaming failed.
Examples
_$ (vl-file-rename "c:/newauto.bat"
"c:/myauto.bat")
T
VL-FILE-SYSTIME
Returns last modification time of the
specified file
(vl-file-systime filename)
Arguments :
filename
- A string containing the name of the
file to be checked.
Return Values
- A list containing the modification
date and time, or nil, if the file is not found.
The list returned contains the following elements :
year
month
day-of-week
day-of-month
hours
minutes
seconds
Note that Monday is day 1 of
day-of-week, Tuesday is day 2, etc.
Examples
_$ (vl-file-systime "c:/program
files/acad2000/sample/visuallisp/yinyang.lsp")
(1998 4 3 8 10 6 52 0)
The returned value shows that the file was last modified in 1998, in the
4th month of the year (April), the 3rd day of the week (Wednesday), on the
10th day of the month, at 6:52:0.
VL-FILENAME-BASE
Returns the name of a file, after
stripping out the directory path and extension
(vl-filename-base filename)
Arguments :
filename
- A string containing a file name. The
vl-filename-base function does not check to see if the file exists.
Return Values
- A string containing filename in
uppercase, with any directory and extension stripped from the name.
Examples
_$ (vl-filename-base "c:\\acadwin\\acad.exe")
"ACAD"
_$ (vl-filename-base "c:\\acadwin")
"ACADWIN"
VL-FILENAME-DIRECTORY
Returns the directory path of a file,
after stripping out the name and extension
(vl-filename-directory filename)
Arguments :
filename
- A string containing a complete file
name, including the path. The vl-filename-directory function does not
check to see if the specified file exists. Slashes (/) and backslashes
(\) are accepted as directory delimiters.
Return Values
- A string containing the directory
portion of filename, in uppercase.
Examples
_$ (vl-filename-directory "c:\\acadwin\\acad.exe")
"C:\\ACADWIN"
_$ (vl-filename-directory "acad.exe")
""
VL-FILENAME-EXTENSION
Returns the extension from a file name,
after stripping out the rest of the name
(vl-filename-extension filename)
Arguments :
filename
- A string containing a file name,
including the extension. The vl-filename-extension function does not
check to see if the specified file exists.
Return Values
- A string containing the extension of
filename. The returned string starts with a period (.) and is in
uppercase. If filename does not contain an extension,
vl-filename-extension returns nil.
Examples
_$ (vl-filename-extension "c:\\acadwin\\acad.exe")
".EXE"
_$ (vl-filename-extension "c:\\acadwin\\acad")
nil
VL-FILENAME-MAKETEMP
Calculates a unique file name to be used
for a temporary file
(vl-filename-mktemp [pattern directory extension])
Arguments :
pattern
- A string containing a file name
pattern; if nil or absent, vl-filename-mktemp uses "$VL~~".
directory
- A string naming the directory for
temporary files; if nil or absent, vl-filename-mktemp chooses a
directory in the following order:
The directory specified in pattern, if any.
The directory specified in the TMP environment variable.
The directory specified in the TEMP environment variable.
The current directory.
extension
- A string naming the extension to be
assigned to the file; if nil or absent, vl-filename-mktemp uses the
extension part of pattern (which may be an empty string).
Return Values
- A string containing a file name, in
the following format :
directory\base<XXX><.extension>
where:
base is up to 5 characters, taken from pattern
XXX is a 3 character unique combination
All file names generated by vl-filename-mktemp during a VLISP session
are deleted when you exit VLISP.
Examples
_$ (vl-filename-mktemp)
"C:\\TMP\\$VL~~004"
_$ (vl-filename-mktemp "myapp.del")
"C:\\TMP\\MYAPP005.DEL"
_$ (vl-filename-mktemp "c:\\acadwin\\myapp.del")
"C:\\ACADWIN\\MYAPP006.DEL"
_$ (vl-filename-mktemp "c:\\acadwin\\myapp.del")
"C:\\ACADWIN\\MYAPP007.DEL"
_$ (vl-filename-mktemp "myapp" "c:\\acadwin")
"C:\\ACADWIN\\MYAPP008"
_$ (vl-filename-mktemp "myapp" "c:\\acadwin"
".del")
"C:\\ACADWIN\\MYAPP00A.DEL"
OK, enough of this theoretical nonsense!
Let's do something practical.
How do you fancy creating your own personalised file list box?
You do? Great! I'll see you in Part II.
|