Jump to content

readdir()


jaesun

Recommended Posts

I quickly created this script to rename a bunch of files, that was renaming a bunch of files based on the season/episode.

 

It grabs the directory listing, check the filename and determine which episode number, and rename the file into the same directory.

 

But sometimes, it would recheck the newly renamed files, essentially twice.

 

It seemed to happen when the directory had 25 or more files.

 

code:

 




$sort_folder = scandir('H:\\Videos\\_Sort\\', 1);
array_pop($sort_folder); // remove .
array_pop($sort_folder); // remove ..
 
foreach($sort_folder as $folder) {
$n = explode('.', $folder);
$year = str_replace('S', '', $n[2]);
$episodes = $seasons[$year]['episodes']; // array that holds all episode titles, episode number for the season
$numFiles = 0;
if ($handle = opendir('H:\\Videos\\_Sort\\' . $folder)) {
while (false !== ($fileName = readdir($handle))) {
if ($fileName == '.' || $fileName == '..') continue;
echo "\n$fileName:\n";
foreach ($episodes as $e) {
if (strripos($fileName, $e['title']) !== false) {
$numFiles++;
echo $fileName . ' => ' . 'Looney.Tunes.s' . $year . '.e' . sprintf('%02d', $e['epnum']) . '.' . $fileName . "\n";
rename('H:\\Videos\\_Sort\\' . $folder . '\\' . $fileName, 
'H:\\Videos\\_Sort\\' . $folder . '\\' 
. 'Looney.Tunes.s' . $year . '.e' . sprintf('%02d', $e['epnum']) . '.' . $fileName);
break;
}
}
}
closedir($handle);
if ($numFiles != count($episodes)) {
echo 'Some Files were not renamed';
}
}
}



 

Some example output:

 



...Normal Expected renames
 
102756 Wideo Wabbit MM CN.avi:
102756 Wideo Wabbit MM CN.avi => Looney.Tunes.s1956.e25.102756 Wideo Wabbit MM CN.avi
 
Looney.Tunes.s1956.e01.011456 Bugs Bonnets MM.mpg:
Looney.Tunes.s1956.e01.011456 Bugs Bonnets MM.mpg => Looney.Tunes.s1956.e01.Looney.Tunes.s1956.e01.011456 Bugs Bonnets MM.mpg
 
...Goes to rename all the files over again one more time


 

If I left only 24 files, it would just rename it all once, as expected.  Add a 25th file to rename, and it would rename them all twice over.

 

I was thinking, well, it just grabs the next  file in the resource handle, and since files are getting renamed, the newly renamed files will come after the old files are cycled through, since, in the resource (being that same directory), the pointer will point to the new file.

 

But then wouldn't that happen regardless of how many files in the directory?  even if there are 28 files, or 5 files.  But it seems to only happen once I reach 25 files in the directory.

 

I checked PHP docs, but saw nothing there that would hint at this behavior?

 

Edit:  Sorry for the bad title.  Hit Submit rather than preview.

Edited by jaesun
Link to comment
Share on other sites

Without trying to follow your convoluted code, I wish to point out and ask one question.  You start, quite properly I believe, by getting a list of the contents of your chosed directory.  Then you start a loop on that array holding those names.  But then you re-open the dir and start plucking filenames again which you already have available to you.  Why?   Seems to me you have what you need and should be simply attaching a handle to each and renaming it if it fits your conditions.

Link to comment
Share on other sites

The folder structure was:

H:\Videos\_Sort\Looney.Tunes.1956.TV\102756 Wideo Wabbit MM CN.avi

 

So the first time I grab a list of contents, I was just grabbing the parent folders (Looney.Tunes.1956.TV).  Then from there, I grabbed the video files within that folder.

 

I am not defending the code, it was late night and something i just kinda copied and pasted for a one time use script (I had like 20 folders of files to rename and I got tired of manually doing them myself).  I know I can change the code easily and not bother going through the list with readdir() and rename during that process. and that I should just grab the video list first, then go about the file renames which would eliminate any issues.

 

I was just intrigued by the behavior of readdir() itself and why it would do that after only a certain number of files was in the directory.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.