Jump to content

Recommended Posts

Posted

Hello,

 

My problem, how can I find a file with a certain prefix and save it on a variable. Asterisk doesn't seams to work. The prefix is "ARCH-*". The file is one the same location with my current drawing file. Thank you.

 

(setq f (findfile (strcat (getvar 'dwgprefix) "ARCH-*.dwg")))  

Posted

If the file exist and has this prefix "ARCH-*.dwg", this should return it's full path and name :

 

(setq f (strcat (getvar 'dwgprefix) (car (vl-directory-files (getvar 'dwgprefix) "ARCH-*.dwg"))))

 

M.R.

Posted (edited)
If the file exist and has this prefix "ARCH-*.dwg", this should return it's full path and name :

 

Nice, M.R. Wildcards don't work with (findfile ...). What you gave us assumes there is only one such .dwg in the directory, right? Is it actually pulling the first occurrence from a list?

Edited by neophoible
missed closing quote
Posted

Thanks Marko, works great!

 

If the file exist and has this prefix "ARCH-*.dwg", this should return it's full path and name :

 

(setq f (strcat (getvar 'dwgprefix) (car (vl-directory-files (getvar 'dwgprefix) "ARCH-*.dwg"))))

 

M.R.

Posted

What you gave us assumes there is only one such .dwg in the directory, right? Is it actually pulling the first occurrence from a list?

 

Not exactly, vl-Directory-Files will return a list of matching strings. The use of Car returns the first entry from the resultant list of string(s), if the list is non-Nill.

 

HTH

Posted
Not exactly, vl-Directory-Files will return a list of matching strings. The use of Car returns the first entry from the resultant list of string(s), if the list is non-Nill.

 

Yes, this is exactly what I had in mind. Thus, if there were, say, accidentally more than one such prefixed DWG, then this would not warn you, it would just take whichever one was first in the returned list and report it. The only way it works for sure is that there is only one such prefixed DWG in the directory. Otherwise, it may or may not return the right one. This is in no way a criticism, BTW, it is simply an observation and possible caveat.

Posted

Well, you can still use the same idea:

(defun FindMultipleFiles (pathwild / path wild)
 (setq path (strcat (vl-filename-directory pathwild) "\\")
       wild (substr pathwild (1+ (strlen path))))
 (mapcar '(lambda (filename) (strcat path filename))
         (vl-directory-files path wild 1)))

Untested, but it should return a list of full pathnames to each file matching the wildcard. E.g.

(FindMultipleFiles (strcat (getvar 'dwgprefix) "ARCH-*.dwg"))

Either returns nil if none found, or a list with each match - so the list's length can be checked if there's more than one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...