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

Findfile question.

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
mid-awe
332 Views, 11 Replies

Findfile question.

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.

11 REPLIES 11
Message 2 of 12
Kent1Cooper
in reply to: mid-awe

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* ...

Kent Cooper, AIA
Message 3 of 12
mid-awe
in reply to: Kent1Cooper

🙂 lol. You are absolutely correct. Once I made your change, it's working well.

Thank you Kent.
Message 4 of 12
Kent1Cooper
in reply to: mid-awe

... 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"?]

Kent Cooper, AIA
Message 5 of 12
mid-awe
in reply to: Kent1Cooper

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 😉

I love the cond method. Thank you again.
Message 6 of 12
Kent1Cooper
in reply to: mid-awe


@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

Kent Cooper, AIA
Message 7 of 12
mid-awe
in reply to: Kent1Cooper

Thank you again. I like "one time done" solutions 😉

This is great and I learned a some lessons too.
Message 8 of 12
dgorsman
in reply to: mid-awe

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.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 9 of 12
mid-awe
in reply to: mid-awe

Thank you. That is a good point. I have another lisp I need for looking up a specific customer's folder that vl-directory-files could be great for.

Again, thank you.
Message 10 of 12
Lee_Mac
in reply to: mid-awe

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"))

 

Message 11 of 12
mid-awe
in reply to: mid-awe

Thank you Lee. That is a great example. 🙂
Message 12 of 12
Lee_Mac
in reply to: mid-awe

You're most welcome mid-awe Smiley Happy

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

Post to forums  

Autodesk Design & Make Report

”Boost