• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1249
  • Last Modified:

PHP: Rename all files in folder

I have a lot of files in two folders, I need a script to rename them [php].

In folder one /images they are all in a form such as "3245 green forest.jpg" and I want them all renamed to, for example "3245_green_forest.jpg".

In folder two /thumbs I want the same starting file ("3245 green forest.jpg") to be renamed to "3245_green_forest_thumb.jpg"

0
level9wizard
Asked:
level9wizard
  • 4
  • 2
1 Solution
 
EMB01Commented:
You will want to do this:

- List all files in directory
- Put all files into an array
- Rename all files with new value

I'll post up a code you can try.
0
 
EMB01Commented:
Here's the code you can try. Be sure to change the $dir variable. Let me know what happens.

The script should:

- Open directory
- Read all files
- Put files into array
- Remove .jpg from all files in array
- Append and update new files names from array
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.gif");
}
 
?>

Open in new window

0
 
EMB01Commented:
Sorry, you'd need to use this for .JPG (not .GIF as I had previously).
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.jpg");
}
 
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
level9wizardAuthor Commented:
This does not do what I asked above. It should replace spaces with _

0
 
EMB01Commented:
I think this is how to replace spaces... Try it out...
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// replace spaces with underscores
foreach ($files as $file) {
    preg_replace("/ /", "_", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.jpg");
}
 
?>

Open in new window

0
 
level9wizardAuthor Commented:
Hmm, that wasn't it either. In the end I made one myself:

<?php
$path = "c:\\wamp\\www\\images";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo "".$path . "\\".str_replace(" ","_",$file);
        rename($path."\\".$file, $path . "\\".str_replace(" ","_",$file));
        $i++;
    }
}
closedir($dh);
?>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now