Whitt Posted May 7 Posted May 7 I'm using vl-directory-files to get a list of files. I know how to use a wildcard to only grab files that contain a certain string of characters (ex. ABR*.dwg) but how do I only grab files that DO NOT contain a string of characters? For instance, grab all .dwg's which do not have "ABR" in their name? Quote
RBrigido Posted May 7 Posted May 7 (edited) How about you try something like this, see if it works. (defun c:test () (setq directory "C:\\Folder\\To\\Your\\Directory\\") (setq files (vl-directory-files directory "*.dwg" 1)) (setq filtered-files '()) (foreach file files (if (not (vl-string-search "ABR" file)) (setq filtered-files (cons file filtered-files)) ) ) (princ "\n.dwg files that do not contain 'ABR' in the name:\n") (foreach file filtered-files (princ (strcat directory file "\n")) ) (princ) ) Edited May 7 by RBrigido Quote
pkenewell Posted May 7 Posted May 7 @RBrigido I am not sure (vl-string-search) will work correctly since it looks at each character in the pattern individually. I recommend using (wcmatch) instead. Also - just a suggestion to shorten code; you could shorten this: (setq filtered-files '()) (foreach file files (if (not (vl-string-search "ABR" file)) (setq filtered-files (cons file filtered-files)) ) ) to This: (setq filtered-files (vl-remove-if '(lambda (x) (wcmatch x "*ABR*")) files)) And, since your already using Visual LISP, you should put (vl-load-com) at the top of your function. Quote
RBrigido Posted May 7 Posted May 7 @pkenewell The change you proposed seems like a great code optimization. The "vl-remove-if function" is specific to removing items from a list based on a condition, which makes it a better choice for this particular case. This approach can also improve code performance in cases of large data sets, as it is more efficient than manually iterating over each list item. I just started with something to try and you make it better 1 Quote
mhupp Posted May 8 Posted May 8 (edited) This will output a window with all matching files so you can pick with a mouse. I use it to insert blocks into a drawing. Block_list.lsp Edited May 8 by mhupp 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.