Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

findfile function?

18 REPLIES 18
Reply
Message 1 of 19
molokaiboy
369 Views, 18 Replies

findfile function?

Does anyone have some code for the findfile function. What I am trying to do is have a lisp seach the working directories for a "VP" or "MS" prefix of the file name. If it is found, I want to have a error message pop up. I got something to work but I can't seem to get it just right. Can anyone help.

TIA

Colliln
18 REPLIES 18
Message 2 of 19
BillZ
in reply to: molokaiboy

(foreach n "file_list"
(if (findfile n)
(alert "This is the one baby.")
)
)
BillZ
Message 3 of 19
Anonymous
in reply to: molokaiboy

Yours is a good example of the importance of letting us know which version
of AutoCAD. In A2K or higher you can use vl-directory-files.
(vl-directory-files [directory pattern directories])
--
Cliff

PS: Wish list - add an argument for optional recurse subdirectories.

"molokaiboy" wrote in message
news:f087784.-1@WebX.maYIadrTaRb...
|
| Does anyone have some code for the findfile function. What I am trying to
do is have a lisp seach the working directories for a "VP" or "MS" prefix of
the file name. If it is found, I want to have a error message pop up. I got
something to work but I can't seem to get it just right. Can anyone help.
|
| TIA
|
| Colliln
|
|
Message 4 of 19
molokaiboy
in reply to: molokaiboy

I am using ACAD 2Ki. Can you post some sample code?

TIA

Collin
Message 5 of 19
Anonymous
in reply to: molokaiboy

(vl-directory-files "k:\\mydir" "MS*.ext" 1)

"molokaiboy" wrote in message
news:f087784.2@WebX.maYIadrTaRb...
| I am using ACAD 2Ki. Can you post some sample code?
| TIA
|
| Collin
|
|
Message 6 of 19
molokaiboy
in reply to: molokaiboy

Thanks for that piece of info. I am not too proficient with VLISP so I am a little lost as to how I would use that piece of code with mine. How would I integrate that function with mine??

TIA

Collin
Message 7 of 19
Anonymous
in reply to: molokaiboy

Well, that's a little hard to answer since you haven't posted your code.
--
Cliff
Message 8 of 19
molokaiboy
in reply to: molokaiboy

(DEFUN C:ckfind()
(setq findmyfile "*vp.dwg")
(setq fil (findfile findmyfile))
(if fil
(setq findmyfile fil)
(alert (strcat "\"FILE FOUND ON WORKING DIRECTORY!!!!! USE PART FROM PARTS LIBRARY" "findmyfile"." ))
))
Message 9 of 19
Anonymous
in reply to: molokaiboy

This will search the current directory for *vp.dwg.

(DEFUN C:ckfind()
(setq findmyfile "*vp.dwg")
(setq fil (vl-directory-files nil findmyfile 1))
(if fil
(setq findmyfile fil)
(alert (strcat "\"FILE FOUND ON WORKING DIRECTORY!!!!! USE PART FROM
PARTS LIBRARY" "findmyfile"." ))
))
Message 10 of 19
molokaiboy
in reply to: molokaiboy

Thanks for the code. But the alert box comes up regardsless of the findings. Even if the files are not in my directory, the I still get the alert box. I would like it to search my c and x drives. Can you help with this problem??

TIA

Collin



(DEFUN C:findfiles()
(setq findmyfile "*vp.dwg")
(setq fil (vl-directory-files "c:" findmyfile 1))
(if fil
(setq findmyfile fil)
(alert (strcat "\"FILE FOUND ON WORKING DIRECTORY!!!!! USE PART FROM PARTS LIBRARY"))
))
Message 11 of 19
Anonymous
in reply to: molokaiboy

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)))
)
Message 12 of 19
molokaiboy
in reply to: molokaiboy

Cliff,

Thank you very much for your help. I tried it and it works great. Is there anyway for all files found to show up?? Also, what do you suggest I do about another alert after the first one comes up to warn the user that something else needs to be done?

TIA

Collin
Message 13 of 19
tom_brabant
in reply to: molokaiboy

Hey guys. This might be a digression but attached is a recurser for making a list of all drawings below a certain folder. Should've used vl-remove instead of "rember" as I've already sacrificed R14 compatability anyway. [usage: (setq the-big-list (c:explore))]
Message 14 of 19
Anonymous
in reply to: molokaiboy

>Thank you very much for your help.

You're welcome.

>Is there anyway for all files found to show up?

The fil variable is a list of all the files. You can do whatever you want
with it.

>Also, what do you suggest I do about another alert after the first one
comes up to warn the user that something else needs to be done?

I wouldn't be too profuse with alerts. They are very annoying to people who
are trying to get work done.
--
Cliff

Good night lisping ghetto.
Message 15 of 19
molokaiboy
in reply to: molokaiboy

How would I list all the files that are found???

TIA

Collin
Message 16 of 19
Anonymous
in reply to: molokaiboy

(defun c:ckfind ( / fil)
(if
(or
(setq fil (acet-file-dir "vp*.dwg" "c:\\"))
(setq fil (acet-file-dir "VP*.dwg" "x:\\"))
)
(progn
(setq fil
(apply 'strcat
(mapcar
'(lambda (file) (strcat "--> " file "\n"))
fil
)
)
)
(alert (strcat "FILE FOUND ON WORKING DIRECTORY!!!!!\nUSE PART FROM PARTS
LIBRARY...\n\n--> " fil))
)
)
(princ)
)
--
Cliff

"molokaiboy" wrote in message
news:f087784.13@WebX.maYIadrTaRb...
| How would I list all the files that are found???
|
| TIA
|
| Collin
|
|
Message 17 of 19
molokaiboy
in reply to: molokaiboy

Ok, now I am totally confused. here is the code that you gave me. I tweaked it a little bit but how can I show a list of all files that were found or how do I put the code that you gave me today into that one.

TIA

Collin


(if
(or
(setq fil (acet-file-dir "vp*.dwg" "c:\\"))
(setq fil (acet-file-dir "VP*.dwg" "x:\\"))
)
(progn
(setq fil
(apply 'strcat
(mapcar
'(lambda (file) (strcat "--> " file "\n"))
fil
)
)
)
(alert (strcat "FILE FOUND ON WORKING DIRECTORY!!!!!\nUSE PART FROM PARTS
LIBRARY...\n\n--> " fil))
)
)
(princ)
)
Message 18 of 19
Anonymous
in reply to: molokaiboy

Replace the entire (if...) statement in the previous one with this one.
--
Cliff

"molokaiboy" wrote in message
news:f087784.15@WebX.maYIadrTaRb...
| Ok, now I am totally confused. here is the code that you gave me. I
tweaked it a little bit but how can I show a list of all files that were
found or how do I put the code that you gave me today into that one.
| TIA
|
| Collin
|
|
| (if
| (or
| (setq fil (acet-file-dir "vp*.dwg" "c:\\"))
| (setq fil (acet-file-dir "VP*.dwg" "x:\\"))
| )
| (progn
| (setq fil
| (apply 'strcat
| (mapcar
| '(lambda (file) (strcat "--> " file "\n"))
| fil
| )
| )
| )
| (alert (strcat "FILE FOUND ON WORKING DIRECTORY!!!!!\nUSE PART FROM
PARTS
| LIBRARY...\n\n--> " fil))
| )
| )
| (princ)
| )
|
|
Message 19 of 19
molokaiboy
in reply to: molokaiboy

I can't get it to even load now. Maybe you can see what is wrong with it.

Thanks,

Collin

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost