Jump to content

How to Exclude Files from vl-directory-files


Whitt

Recommended Posts

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?

Link to comment
Share on other sites

Posted (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 by RBrigido
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

@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

 

  • Like 1
Link to comment
Share on other sites

Posted (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.

 

image.png.fcae989a6fc9a31c87620164748e0121.png

 

 

 

Block_list.lsp

Edited by mhupp
Link to comment
Share on other sites

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