root/clean.php

Revision 2914, 1.3 kB (checked in by pmjones, 9 months ago)

updated comments

  • Property svn:executable set to
  • Property svn:keywords set to Id
Line 
1 <?php
2 /**
3  * Gets a list of files from a directory.
4  * Recursively descends into subdirectories.
5  * Only lists files starting with a dot (not inluding .svn files).
6  *
7  * @todo replace this with shell script `find . -type f -name "\.*"` ...
8  * be sure to check exit status.
9  *
10  * @todo replace with `find . -type f -name "\.*" -exec rm {} \;` instead?
11  *
12  * @todo replace with `find . -type f -name "\.*" -print -delete` instead?
13  *
14  */
15 function getBadFiles($base, $dir = null)
16 {
17     if ($dir === null) {
18         $dir = $base;
19     }
20     
21     $list = array();
22     foreach (scandir($dir) as $name) {
23     
24         if ($name == '.' || $name == '..' || $name == '.svn') {
25             // skip!
26             continue;
27         }
28         
29         $result = "$dir/$name";
30         
31         if (is_dir($result)) {
32             // recurse into the subdirectory
33             $sub = getBadFiles($base, $result);
34             $list = array_merge($list, $sub);
35         } else {
36             // only get names starting with a dot
37             if (substr($name, 0, 1) == '.' ||
38                 substr($name, -1) == '~') {
39                 $list[] = $result;
40             }
41         }
42     }
43     
44     asort($list);
45     return $list;
46 }
47
48 // now actually delete matching files
49 foreach (getBadFiles('.') as $name) {
50     echo "$name\n";
51     unlink($name);
52 }
Note: See TracBrowser for help on using the browser.