Hi all,
I'm trying to find a file with one of several suffixes and then return the name of the file if found.
(IF (or (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) ".pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r.pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r2.pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r3.pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r4.pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r5.pdf"))) (setq *PDFN* (FINDFILE (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) "r6.pdf"))) ) (if (= (DOS_MSGBOX "Insert Scanned Drawing?" "Import PDF" 4 4) 6) (VL-CMDF "-PDFATTACH" (STRCAT "U:\\plans\\scaleddrawings\\" *PDFN*) "1" PAUSE 1.0 0)) )
As is nothing is found even if it exists. Do I need a foreach loop instead?
Any help is greatly appreciated.
Thank you in advance.
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Is it really not found, or just not imported? Since the return from (findfile) that you put in the *PDFN* variable will already include the entire path, I think this part:
... (VL-CMDF "-PDFATTACH" (STRCAT "U:\\plans\\scaleddrawings\\" *PDFN*) ...
should be just this instead:
... (VL-CMDF "-PDFATTACH" *PDFN* ...
... but yes, the search could be condensed, something like [untested]:
(setq
basefile (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4) ".pdf")
*PDFN*
(cond
((findfile (strcat basefile "r6.pdf"))); returns nil if not found, go on to look for next
((findfile (strcat basefile "r5.pdf")))
((findfile (strcat basefile "r4.pdf")))
((findfile (strcat basefile "r3.pdf")))
((findfile (strcat basefile "r2.pdf")))
((findfile (strcat basefile "r.pdf")))
((findfile (strcat basefile ".pdf")))
); cond & *PDFN* -- nil if none of the above variants is found
); setq
(if
(and
*PDFN* ; one of them was found
(= (DOS_MSGBOX "Insert Scanned Drawing?" "Import PDF" 4 4) 6)
); and
(VL-CMDF "-PDFATTACH" *PDFN* "1" PAUSE 1.0 0); 'then'
); if
EDIT: I reversed the order of the (findfile) name endings, since it looks like the effect of your original would be to use the largest-suffix-number item found. I hope they wouldn't go beyond 6.
FURTHER EDIT: No, forget that conclusion [the (or) function will be satisfied and stop looking once it finds one, so the lowest-number one will be in the variable], but I didn't change the order back, because wouldn't you always want to use the latest Revision [assuming that's what the r stands for] if there have been any? [Also, might a first Revision ever be called "r1" instead of just "r," so should there also be a line for the possibility of "r1"?]
@mid-awe wrote:
correct you are on the need to find the largest number if it exists.
Yes about the r1, but we don't expect revisions therefore rev1 is simply r then r2 r3 r4 and so on. This far we don't know of any revisions beyond 4 in years. I added to r6 just in case, but an infinite would be better; I didn't want to type until tired of typing 😉
....
You could set it to start as high as you like, though the approach is largely different from the (cond) way:
(setq
basefile (STRCAT "U:\\plans\\scaleddrawings\\" (SUBSTR (GETVAR 'DWGNAME) 5 4))
n 50 ; or however high a number you want
); setq
(while
(and
(not (setq *PDFN* (findfile (strcat basefile "r" (itoa n) ".pdf")))); not there with that number
(> n 0); haven't hit bottom yet [allows for possible "r1" just in case, without additional code]
); and
(setq n (1- n)) ; look at next number down
); while
(if (not *PDFN*); didn't find it with a numerical end
(setq *PDFN*
(cond
((findfile (strcat basefile "r.pdf"))); revision without number
((findfile (strcat basefile ".pdf"))); file without revision
); cond & *PDFN* -- nil if none of the above variants is found
); setq
); if
(if
(and
*PDFN* ; one of them was found
(= (DOS_MSGBOX "Insert Scanned Drawing?" "Import PDF" 4 4) 6)
); and
(VL-CMDF "-PDFATTACH" *PDFN* "1" PAUSE 1.0 0); 'then'
); if
Don't forget about (vl-directory-files ...). It won't do nested folders automatically but it can also return folders to generate a nested or recursive search, and it will do wildcard file searches.
I would also recommend vl-directory-files for this task - here is a quick example to retrieve the required PDF filename (with no counters):
(defun pdfattach ( dir pat / pdf ) (if (setq pdf (car (vl-sort (vl-directory-files dir pat 1) '(lambda ( a b ) (> (atoi (getrevision (vl-filename-base a))) (atoi (getrevision (vl-filename-base b))) ) ) ) ) ) (strcat dir "\\" pdf) ) ) (defun getrevision ( s ) (substr s (1+ (strlen (vl-string-right-trim "0123456789" s)))) )
For your example, you would call the function in the following way:
(pdfattach "U:\\plans\\scaleddrawings" (strcat (substr (getvar 'dwgname) 5 4) "*.pdf"))
You're most welcome mid-awe