max
max

Reputation: 3726

copy all the files that end with specific name to another directory

this is how i save image and create thumbnails for it , each image may have different thumbnail with different dimensions

    original image :
     abc.jpg

    thumbnails :

    100X150_abc.jpg
    80X80_abc.jpg
etc...

now i've made a mistake and about couple hundreds of my images has been saved in the wrong directory i know the name of the original images but i'm not sure about thumbnails

all my images have a unique name so i can identify the thumbnails with their name but the beginning part of the name in thumbnails is dynamic .

lets say i want to copy my abc.jpg from 06 directory in the 07 directory

rename('images/06/abc.jpg', 'images/07/abc.jpg');

but what can i do about thumbnails ? is there a regex like way for doing this ?

like !

rename('thumbs/06/^_abc.jpg', 'thumbs/07/^_abc.jpg');

basically i want to copy all of the images that end with specific name ( like _abc.jpg) to another directory

Upvotes: 1

Views: 696

Answers (3)

flori
flori

Reputation: 15867

The glob() function supports * as wildcard (but no full regex support). It returns an array of matched file names:

$from = '/absolute/path/thumbs/06/';
$to   = '/absolute/path/thumbs/07/';
chdir($from); // in this way glob() can give us just the file names
foreach(glob('*_abc.jpg') as $name) {
    rename($from.$name, $to.$name);
}

The glob() function is not so well known, but very handy in such situations because other PHP file functions doesn't support stuff like wildcards.

Upvotes: 3

user2635745
user2635745

Reputation:

Use rename('images/06/*abc.jpg', 'images/07/*abc.jpg'); and if you want just the Thumbnails: rename('images/06/*_abc.jpg', 'images/07/*_abc.jpg'); btw if you want all files that begin with abc use abc*.jpg

Upvotes: 0

SRKR
SRKR

Reputation: 33

Here is the code that should work:

<?php
$olddir = "./images/06/";
$newdir = "./images/07/";
$files = scandir($olddir);
$count = count($files);
echo $count;
$x = 2;
while($x < $count)
    {
    echo $files[$x];
    $len = strlen($files[$x]);
    $sub = substr($files[$x], $len-7, 7);
    if($sub === "abc.jpg")
        {
        rename($olddir.$files[$x],$newdir.$files[$x]);
        }
    $x++;
    }
?>

This will move all the files in 06 folder that end with abc.jpg to 07 folder.

Let me know if it doesn't work.... :)

Upvotes: 1

Related Questions