File Search

AutoCAD AutoLISP & Visual LISP

 
File Search
 
 
 

An application can use the findfile function to search for a particular file name. The application can specify the directory to search, or it can use the current AutoCAD library path.

In the following code fragment, findfile searches for the requested file name according to the AutoCAD library path:

(setq refname "refc.dwg")
(setq fil  (findfile refname))
(if fil 
  (setq refname fil)
  (princ (strcat "\nCould not find file " refname ". " ))
)

If the call to findfile is successful, the variable refname is set to a fully qualified path name string, as follows:

"/home/work/ref/refc.dwg"

When specifying a path name, you must precede the backslash (\)with another backslash so the path name will be recognized by AutoLISP. Alternatively, you can use the slash character (/) as a directory separator.

The getfiled function displays a dialog box containing a list of available files of a specified extension type in the specified directory. This gives AutoLISP routines access to the AutoCAD Get File dialog box.

A call to getfiled takes four arguments that determine the appearance and functionality of the dialog box. The application must specify the following string values, each of which can be nil: a title, placed at the top of the dialog box; a default file name, displayed in the edit box at the bottom of the dialog box; and an extension type, which determines the initial files provided for selection in the list box. The final argument is an integer value that specifies how the dialog box interacts with selected files.

This simple routine uses getfiled to let you view your directory structure and select a file:

(defun C:DDIR ( )
  (setq dfil (getfiled "Directory Listing" "" "" 2))
  (princ (strcat "\nVariable 'dfil' set to selected file " dfil ))
  (princ)
)

This is a useful utility command. The dfil variable is set to the file you select, which can then be used by other AutoLISP functions or as a response to a command line prompt for a file name. To use this variable in response to a command line prompt, enter !dfil.

NoteYou cannot use !dfil in a dialog box. It is valid only at the command line.

For more information, see getfiled in the AutoLISP Reference.