I have a lot, but a lot of gifs (about five thousand), in several folders (categories names) inside a main folder gifs/
PROBLEM
I need to rename all the gifs for a simple correlative number. for example image (24).gif
to 213.gif
(correlative to the main directory sequence)
I return the whole gifs listing with this function
PHP FUNCTION
$dir = '../gifs';
function listFolderFiles($dir)
{
echo '<ol>';
foreach (new DirectoryIterator($dir) as $i => $fileInfo) {
if (!$fileInfo->isDot()) {
echo '<li>' . $fileInfo->getFilename();
if ($fileInfo->isDir()) {
listFolderFiles($fileInfo->getPathname());
}
echo " - >".$i.'</li>';
}
}
echo '</ol>';
}
listFolderFiles($dir);
//rename('picture.gif', '1.gif');
this is some of the files estructures, most of all have parenthesis on their names...
hugs
animaton (11).gif
animaton (12).gif
animaton (13).gif
animaton (17).gif
animaton (18).gif
animaton (19).gif
animaton (22).gif
animaton (23).gif
animaton (26).gif
animaton (27).gif
animaton (29).gif
animaton (31).gif
animaton (34).gif
animaton (35).gif
animaton (4).gif
animaton (41).gif
animaton (43).gif
animaton (46).gif
animaton (49).gif
animaton.gif
bored
animaton (1).gif
animaton (13).gif
animaton (17).gif
animaton (18).gif
animaton (19).gif
animaton (20).gif
animaton (25).gif
animaton (27).gif
animaton (29).gif
animaton (31).gif
animaton (33).gif
animaton (35).gif
animaton (36).gif
animaton (40).gif
animaton (41).gif
animaton (43).gif
animaton (45).gif
animaton (49).gif
animaton (5).gif
animaton (7).gif
animaton (9).gif
wink
animaton (11).gif
animaton (12).gif
animaton (21).gif
animaton (27).gif
animaton (3).gif
animaton (35).gif
animaton (36).gif
animaton (38).gif
animaton (39).gif
animaton (4).gif
animaton (43).gif
animaton (8).gif
sleepy
animaton (11).gif
animaton (12).gif
animaton (13).gif
animaton (14).gif
animaton (16).gif
animaton (17).gif
animaton (19).gif
animaton (20).gif
animaton (21).gif
animaton (22).gif
animaton (24).gif
animaton (26).gif
animaton (27).gif
animaton (29).gif
animaton (3).gif
animaton (32).gif
animaton (34).gif
animaton (35).gif
animaton (36).gif
animaton (37).gif
animaton (38).gif
animaton (39).gif
animaton (4).gif
animaton (40).gif
animaton (41).gif
animaton (45).gif
animaton (46).gif
animaton (49).gif
animaton (5).gif
animaton (6).gif
animaton (9).gif
animaton.gif
love
animaton (1).gif
animaton (11).gif
animaton (13).gif
animaton (14).gif
animaton (17).gif
animaton (18).gif
animaton (20).gif
animaton (21).gif
animaton (24).gif
animaton (25).gif
animaton (27).gif
animaton (28).gif
animaton (29).gif
animaton (38).gif
animaton (39).gif
animaton (4).gif
animaton (40).gif
animaton (41).gif
animaton (44).gif
animaton (48).gif
animaton (5).gif
animaton (7).gif
animaton (9).gif
animaton.gif
claps
animaton (1).gif
animaton (10).gif
animaton (11).gif
animaton (12).gif
animaton (13).gif
animaton (15).gif
animaton (16).gif
animaton (17).gif
animaton (19).gif
animaton (20).gif
animaton (21).gif
animaton (22).gif
animaton (23).gif
animaton (25).gif
animaton (28).gif
animaton (3).gif
animaton (30).gif
animaton (31).gif
animaton (34).gif
animaton (35).gif
animaton (38).gif
animaton (39).gif
animaton (4).gif
animaton (40).gif
animaton (41).gif
animaton (42).gif
animaton (43).gif
animaton (44).gif
animaton (46).gif
animaton (47).gif
animaton (49).gif
animaton (5).gif
animaton (6).gif
animaton (7).gif
animaton (8).gif
scared
animaton (1).gif
animaton (10).gif
animaton (12).gif
animaton (13).gif
animaton (15).gif
animaton (16).gif
animaton (17).gif
animaton (18).gif
animaton (21).gif
animaton (24).gif
animaton (33).gif
animaton (39).gif
animaton (40).gif
animaton (46).gif
animaton (6).gif
Disclaimer!!
I have tested this, but my behavior may not be what you want.
This function will iterate recursively through every file and subfolder from the directory you give it. It will then change the names of any file by number (consecutive count) in every folder.
I have commented out the rename() function so as to not rename stuff until you are happy with the results. Once you uncomment out the rename() function it will rename EVERY FILE. Hence if it does not work how you want it, there is no going back. This could cause serious havoc to your folder/file structure.
You should back up your files before proceeding just in case.
$dir = '../gifs';
function listFolderFiles($dir){
$results = new DirectoryIterator($dir);
echo '<ul>';
$count = 1;
foreach($results as $fileInfo){
if($fileInfo->isFile()){
$oldFileName = $fileInfo->getPathname();
$newFileName = $fileInfo->getPath() . '/' . $count . '.gif';
//rename($oldFileName, $newFileName); //<-- Uncomment here to actually
//rename your files. Only do this when you are sure you are happy
//with the results.
echo '<li>' . 'File Old: ' . $oldFileName . ' New: ' . $newFileName . '</li>';
$count++;
}
if($fileInfo->isDir() && !$fileInfo->isDot()){
echo '<li>' . 'Folder: ' . $fileInfo->getPathname() . '</li>';
listFolderFiles($fileInfo->getPathname());
}
}
echo '</ul>';
}
listFolderFiles($dir);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.