Page 1 of 1

deleting files that match a pattern

Posted: Sun Aug 07, 2005 9:10 am
by jaymoore_299
How do I delete all files that match a pattern for use on my local computer? For example, I have a whole bunch of files and want to get rid of all files that have the string "notes" or "new documents"

Posted: Sun Aug 07, 2005 9:11 am
by feyd
glob() can easily get you a list of matching wildcards.. you can then iterate over it to delete/traverse the tree as needed..

Posted: Sun Aug 07, 2005 10:43 am
by Chris Corbyn
It depends if you're talking about text within the file itself or within the filename. It also depends if you're on Windows or Linux. Linux is easy --> use grep in a pipe for a non-php method for deleting files based on contents. If you want to use PHP for that same thing then you'll need to open each file and depending upon the number of files that may be pretty slow.

If you're simply deleting them based upon filename then without PHP... well, it's just a case of using wildcards in command line. With PHP you'd open the folder and loop over it contents checking for patterns with strstr() (or glob()) or preg_ ;)

If you need an example snippet then let us know exactly what you need to do :)