deleting files that match a pattern

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

deleting files that match a pattern

Post 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"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
Post Reply