Advertisement
Guest User

Delete all from multiple folders with exceptions

a guest
Jan 7th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.         <title>File deleter</title>
  6.     </head>
  7.     <body>
  8.         <h1>This page should delete all files in the specified folders, except the specified file names. In theory. Not tested.</h1>
  9.         <?php
  10.             $CompletePath=true;
  11.             //Change the above to false if your folders are all in the same parent folder. True means that yes, you're going to give complete paths in $FolderList.
  12.            
  13.             $FolderList=array(
  14.                 "FOLDER 1",
  15.                 "FOLDER 2",
  16.                 "FOLDER 3"
  17.                 //[...]
  18.             );
  19.             //Place all of your folders above. Make sure each folder is between quotes, and each of them EXCEPT THE LAST is followed by a comma.
  20.             //If your folders are NOT in the same parent folder, what you should put between the quotes is their full path name, starting with C:\ or whatever your drive is.
  21.            
  22.             if($CompletePath==false) {
  23.                 $ParentPath="C:\Users\Administrator\Documents\Your parent folder";
  24.             }
  25.             //If all of your folders DO have the same parent folder, put the path to the parent folder in question here. Having the same parent folder means they're inside the same folder, side by side. If not, don't worry about it, it won't even be read.
  26.            
  27.             $FilesToSave=array(
  28.                 "FILE 1.txt",
  29.                 "FILE 2.rar",
  30.                 "FILE 3.png"
  31.             );
  32.             //Put the file names you do NOT want to delete here. Make sure each file is between quotes, and each of them EXCEPT THE LAST is followed by a comma. File names only. No folders.
  33.            
  34.             //------------------You're done with the settings. Below is just code. You should be able to run this now, with WAMP.---------------------//
  35.            
  36.             foreach($FolderList as $Path) {
  37.                 if($CompletePath==true) {
  38.                     $Path=$ParentPath.$Path;
  39.                 }
  40.                 $D_Folder=opendir($Path);
  41.                 while($File=readdir($D_Folder)) {
  42.                     if($File!='.' && $File!='..' && $File!='Thumbs.db') {
  43.                         $OkayToDelete=true;
  44.                         $FileNb=0;
  45.                         while($FileNb<count($FilesToSave) && $OkayToDelete==true) {
  46.                             if($File==$FilesToSave[$FileNb]) {
  47.                                 $OkayToDelete=false;
  48.                             }
  49.                         }
  50.                         if($OkayToDelete==true) {
  51.                             if(is_readable(realpath($Path.'\\'.$File)) {
  52.                                 if(unlink($Path)) {
  53.                                     echo '
  54.                                         <p>File "'.realpath($Path.'\\'.$File).'" was successfully deleted.</p>
  55.                                     ';
  56.                                 }
  57.                                 else {
  58.                                     echo '
  59.                                         <p>Unable to delete file "'.realpath($Path.'\\'.$File).'". Either such a file does not exist, either an unknown error has occurred.</p>
  60.                                     ';
  61.                                 }
  62.                             }
  63.                             else {
  64.                                 echo '
  65.                                     <p>Unable to read file "'.realpath($Path.'\\'.$File).'".</p>
  66.                                 ';
  67.                             }
  68.                         }
  69.                         else {
  70.                             echo '
  71.                                 <p>File "'.realpath($Path.'\\'.$File).'" was not deleted because it was defined as a file to save.</p>
  72.                             ';
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         ?>
  78.     </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement