LISP2LEARN Posted August 22, 2012 Posted August 22, 2012 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"))) Quote
marko_ribar Posted August 22, 2012 Posted August 22, 2012 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. Quote
neophoible Posted August 22, 2012 Posted August 22, 2012 (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 August 22, 2012 by neophoible missed closing quote Quote
LISP2LEARN Posted August 22, 2012 Author Posted August 22, 2012 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. Quote
BlackBox Posted August 23, 2012 Posted August 23, 2012 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 Quote
neophoible Posted August 26, 2012 Posted August 26, 2012 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. Quote
irneb Posted August 26, 2012 Posted August 26, 2012 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. Quote
Recommended Posts
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.