imperium2335 Posted April 6, 2009 Share Posted April 6, 2009 Hi, I've nearly got my mini php app perfect. I have it list all the files that exist in the directory and add those to an array, then later it checks to see if the new name suggested matches any of them, if not, then it can go ahead with the file renaming. But for some reason it trys to rename anyway sometimes and comes back with the error: The name was bday-bespoke.jpg. Attempting to rename to birthday-unique.jpg Warning: rename(tests/bday-bespoke.jpg,tests/birthday-unique.jpg) [function.rename]: File exists in C:\webserver\Apache2\htdocs\Cake-Photos.com\mass-rename-mk2.php on line 119 Success or The name was bday-cakes-colourful.jpg. Attempting to rename to bday-lovely.jpg Error, name already exists Trying celebration-colourful-unique.jpg Error, name already exists Trying bday-bespoke-unique.jpg Warning: rename(tests/bday-cakes-colourful.jpg,tests/bday-bespoke-unique.jpg) [function.rename]: No such file or directory in C:\webserver\Apache2\htdocs\Cake-Photos.com\mass-rename-mk2.php on line 104 Error, name already exists Trying celebration-cakes-fun.jpg Warning: rename(tests/bday-cakes-colourful.jpg,tests/celebration-cakes-fun.jpg) [function.rename]: No such file or directory in C:\webserver\Apache2\htdocs\Cake-Photos.com\mass-rename-mk2.php on line 104 3 attempts to come up with a new name for the file have failed. Increase the number of attempts or there may not be enough words in the pool to create a unique combination. Increase the word pool or word length. Fail Here is my script: <?PHP $Allow_Duplicates = 0 ; // Set to 1 to allow duplicate words in file names. $Rename_Extensions = 0 ; //Set to 1 to allow renaming of extensions. $New_Ext = "my extension" ; //New extension, if rename extensions is on. $retrys = 3 ; //If duplicates exist, how many times should it try to come up with a new combination of words. Caution, setting this number too high when impossible combinations exist, may effect system performance (recommended max of 10 for many files). $min = 1 ; //The minimum number of words in the file name (0 is one word). $max = 3 ; //The maximum number of words in the file name (0 is one word). $dir = "tests" ; //Source directory. RELATIVE TO THIS SCRIPTS LOCATION. $outdir = "tests" ; //Output directory. Same as above. $first_words = array("birthday-","bday-","celebration-") ; //One word will be picked from the array to be used as the first word in the file name. $word_box = array("cake-","celebration-","party-","unique-","bespoke-","fun-","designer-","beautiful-","modern-","designs-","cakes-","colourful-","lovely-") ; //Words in here will be picked at random and inserted into the file name. $first_word_amount = count($first_words) -1 ; //Prevent count of last array object, no such object exists. They exist as 0, 1 and 2 etc (no last N). $word_box_amount = count($word_box) -1 ; //Same as above. $file_count = 0 ; $openzone = @opendir($dir) ; //@ Disable PHPs default error message. if($openzone == FALSE) //If the directory doesn't exist... {echo "The specified directory $dir is invalid" ; exit ;} else {echo "<strong>Mass renamer activated.</strong><br />" ;} while($old_file_name = readdir($openzone)) { if($old_file_name != '.' && $old_file_name != '..' && !is_dir($dir.'/'.$old_file_name)) { $file_count++ ; $old_files .= $old_file_name ; } } rewinddir($openzone) ; while($old_file_name = readdir($openzone)) // While files exist in the source directory... { if($old_file_name != '.' && $old_file_name != '..' && !is_dir($dir.'/'.$old_file_name))//If not current directory, not parent directory and only if it is a file (not a directory)... { $existing = array_unique(explode(" ", $old_files)) ; $rename_attempts = 0 ; //$old_files .= $old_file_name ; //. " " . $new_name . " " ; $fws = rand(0,$first_word_amount) ; //Array selector. $first_word[$fws] $length = rand($min, $max) ; //Random name length between min and max words. $new_name = $first_words[$fws] ; //Start the new file name with the word (array item) we chose at random. if($Rename_Extensions ==0) {$ext = end(explode(".", $dir . "/" . $old_file_name)) ;} //Use the files current extension. else {$ext = $New_Ext ;} //Use the new extension you specified above. for($i = 0; $i < $length ; $i++) //While the counter is less than the set number of words... { $wbs = rand(0,$word_box_amount) ; //Array selector. $word_box[$wbs] $new_name .= $word_box[$wbs] ; //Add a random word from our word pool to the end of the new file name. } if($Allow_Duplicates == 0) {$new_name = implode('-',array_unique(explode('-',$new_name)));} //Remove duplicate words. $new_name = substr($new_name, 0, -1) ; //Remove the end "-". $new_name .= "." . $ext ; // Add the file extension to the new name. echo "<br /><br />The name was $old_file_name" ; echo ". Attempting to rename to $new_name" ; //$existing = explode(" ", $old_files) ; while(in_array($new_name, $existing) && $Allow_Duplicates == 0 && $rename_attempts < $retrys) { echo "<br />Error, name already exists" ; $rename_attempts++ ; $fws = rand(0,$first_word_amount) ; $length = rand($min, $max) ; $new_name = $first_words[$fws] ; for($i = 0; $i < $length ; $i++) //While the counter is less than the set number of words... { $wbs = rand(0,$word_box_amount) ; //Array selector. $word_box[$wbs] $new_name .= $word_box[$wbs] ; //Add a random word from our word pool to the end of the new file name. } $new_name = implode('-',array_unique(explode('-',$new_name))); $new_name = substr($new_name, 0, -1) ; $new_name .= "." . $ext ; echo "<br />Trying $new_name" ; if(!in_array($new_name, $existing)) { rename($dir.'/'.$old_file_name, $dir.'/'.$new_name) ; $renamed++ ; $old_files .= $new_name . " " ; $existing = array_unique(explode(" ", $old_files)) ; } } if($rename_attempts == $retrys) { echo "<br />$retrys attempts to come up with a new name for the file have failed. Increase the number of attempts or there may not be enough words in the pool to create a unique combination. Increase the word pool or word length.<br /><strong>Fail</strong>" ; $fail++ ; } if(!in_array($new_name, $existing)) { rename($dir.'/'.$old_file_name, $dir.'/'.$new_name) ; $renamed++ ; $old_files .= $new_name . " " ; } if($rename_attempts != $retrys) { //$renamed++ ; echo "<br /><strong>Success</strong>" ; } } } if($file_count == NULL) {echo "No files found." ;} else {echo "<br />Out of $file_count files, $renamed files where renamed" ; if($fail > 0){echo " and $fail failed.";}else{echo ".";}} ?> Any advise and critic all welcome and appreciated! thanks Its also reporting the wrong number of renames and fails (i think): Out of 119 files, 266 files where renamed and 98 failed. Link to comment https://forums.phpfreaks.com/topic/152749-file-rename-errors-logic/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.