AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Locating Files.

AutoLISP has two functions available to help us locate files.
They are the (findfile) function and the (getfiled) function.

(findfile).

The (findfile) function will only search the current AutoCAD search path if a drive/directory prefix is not supplied.
If the file is found, the full directory path is returned.

If no file is found, (findfile) returns nil.
The syntax of this function is as follows :

	(findfile "filename")

Say you were looking for the file ACADR14.LSP.

	(findfile "ACADR14.LSP)

Would return :

	"C:\\ACADR14\\SUPPORT\\ACADR14.LSP"

Note :
AutoLisp allows you to use / or \\ for directory descriptors.


(getfiled).

The (getfiled) function will prompt the user for a file name using the standard AutoCAD file dialogue box. It will then return the file name either with the full path name or with the path name stripped.
The syntax of the (getfiled) function is as follows :

(getfiled "Title" "Directory Path and/or File name" "File Extension" Flag)

The "Title" argument is simply the name that will appear in the Title Bar of the dialogue box.

The "Directory Path and/or File Name" argument is the default directory path that the dialogue box will use. If a file name is included this name will appear in the File Name edit box. This can be null.

The "File Extension" function is the default file name extension. If it is null, it defaults to * (all file types). If the file type "dwg" is part of the "File Extension", an image preview is displayed.

There are four possible flags and they make use of the "sum of the flags" concept.
To combine any of the flags, just add them together. The flags are :

Flag 1.	If you set this flag, the function will prompt for the name of a
	NEW file to create.
Flag 4.	This flag will disable the "Type it" button. The user must then
	pick from the listed files rather than type a file name.
	If this flag is not set and the user selects the "Type it" button,
	the dialogue box disappears and (getfiled) returns a value of 1.
Flag 3.	This flag allows the user to enter a file extension.
	If the flag is not set, only the extension used in the extension
	edit box will be used.
	The extension is also added to the file name if the user does not
	enter it in the File Name edit box.
Flag 8.	If this flag is set and flag 1 is not set, (getfiled) searches in 
	accordance to the AutoCAD library search path. It also strips the
	path and only returns the file name.
	If it is not set, it returns the entire path name.

 


Let's look at an example. We want to open a directory on c:/ called "Slides" containing a list of file that we would like to view. Our routine would look like this :
(defun c:slv ( / sl)
	(setq sl (getfiled "Pick a slide to view"
			   "C:/SLIDES/"
			   "sld"
			   10
		 );getfiled
	);setq
	(command "vslide" sl)
   (princ)
);defun
(princ)

Your dialogue box should look like this :

Getfiled

Take note of a couple of things.
See how it defaults to the C:/SLIDES directory;
The "Type it" button has been disabled; (Flag 2 was set.)
The full path name of the file was returned. (Flag 8 was set)
This is because C:/SLIDES is not in my AutoCAD search path.

 


As you can see, this is quite a useful function in that it can restrict your user to only certain directories and file types whilst still leaving them some flexiblity in their choice.
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright © 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website