Even if the files are not in my directory, the I still get the alert
box.
That's what you programmed it to do. If you want to show the alert when the
file is found make it the first expression in the if statement
(if fil ;;;if the file is found
(alert (strcat "\"FILE FOUND ON WORKING DIRECTORY!!!!! USE PART FROM PARTS
LIBRARY")) ;;then alert
(setq findmyfile fil) ;;else
)
Possibly you want to execute both expressions in which case you would use
progn to make one expression.
(if fil ;;;if the file is found
(progn ;;then alert and do setq
(alert (strcat "\"FILE FOUND ON WORKING DIRECTORY!!!!! USE PART FROM PARTS
LIBRARY"))
(setq findmyfile fil)
)
)
I would like it to search my c and x drives. Can you help with this
problem??
The vl-directory-files function will not recurse through subdirectories. If
you have express tools installed there is a function called acet-file-dir
that will recurse. Here is the syntax.
(acet-file-dir pattern [attributes] [starting-directory])
If you want to search your entire c: drive for dwg files with VP as prefix.
Note the asterisk(*) follows the prefix.
(acet-file-dir "VP*.dwg" "c:\\")
;Finally, for the amusement of the experienced programmers and instruction
of the neos, here is a home-made recursive file finder. It is very slow
(try comparing it to acet-file-dir), but was an interesting exercise. In
your case the call would be:
(search-dirs "c:\\" "vp*.dwg")
So, since I have totally confused you by now, here are two versions of your
function - one using acet-file-dir and the other search-dirs. Note that if
there are more than one drawing with the VP prefix only the first one found
will be shown.
(defun c:ckfind ( / fil)
(if
(or
(setq fil (acet-file-dir "vp*.dwg" "d:\\"))
(setq fil (acet-file-dir "VP*.dwg" "x:\\"))
)
(progn
(setq findmyfile fil)
(alert (strcat "FILE FOUND ON WORKING DIRECTORY!!!!!\nUSE PART FROM PARTS
LIBRARY...\n\n--> " (car fil)))
)
)
(princ)
)
(defun c:ckfind ( / fil)
(if
(or
(setq fil (search-dirs "c:\\" "vp*.dwg"))
(setq fil (search-dirs "x:\\" "vp*.dwg"))
)
(progn
(setq findmyfile fil)
(alert (strcat "FILE FOUND ON WORKING DIRECTORY!!!!!\nUSE PART FROM PARTS
LIBRARY...\n\n--> " (car fil)))
)
)
(princ)
)
;________________________________________________________________________
;recursive file finder
;recurses through all subdirectories
;(acet-file-dir filename 0 dir) does faster job than search-dirs
(defun search-dirs (path file)
(vl-remove nil
(flatten
(search-dirs-aux path file)
)
)
)
(defun search-dirs-aux (path file)
(cons
(mapcar '(lambda (filename) (path-append path filename))
(vl-directory-files path file))
(mapcar
(function
(lambda (dir)
(if (/= (substr dir 1 1) ".")
(search-dirs-aux (path-append path dir) file)
)
)
)
(vl-directory-files path nil -1) ;SUBDIRECTORIES
)
)
)
;; Add element (filename or subdirectory) to path:
;; by Tony Tanzillo
(defun path-append (path item)
(if (eq (substr path (strlen path)) "\\")
(strcat path item)
(strcat path "\\" item)
)
)
(defun flatten (x)
(if (atom x) (list x) (apply 'append (mapcar 'flatten x)))
)