Jump to content

Renama files under stubdirectory's..


xam

Recommended Posts

hello,

I've over 500 files with same names (with same specialtag) under many dirs + subdirectorys

example:
d:/ftp/directory1/subdirectory/specialtag_filename1.gif
d:/ftp/directory2/subdirectory/subdirectory2/specialtag_filename1.psd
d:/ftp/directory2/subdirectory2/subdirectory3/specialtag_filename2.jpg

how do I change the specialtag front of each file?
example:
d:/ftp/directory2/subdirectory/subdirectory2/[b]specialtag[/b]_filename1.psd
to
d:/ftp/directory2/subdirectory/subdirectory2/[b]xam[/b]_filename1.psd

or

d:/ftp/directory2/subdirectory2/subdirectory3/[b]specialtag[/b]_filename2.jpg
to
d:/ftp/directory2/subdirectory2/subdirectory3/[b]xam[/b]_filename2.jpg

is this possible with php?

regards,
xam.
Link to comment
Share on other sites

This is very rough and untested. I hacked this together from some of my old code.
The parts of the filename you want to change will be put in the subdirectory array.

[code]
<?php

$extensions = array(".psd");
$ignore = array(".", "..");

$subdirectory = array(
"directory2/subdirectory/subdirectory2" => array("from" => "specialtag_", "to" => "xam_")
);

public function parseList($dir="") {
global $extensions, $ignore, $subdirectory;

// Add a / if required
if($dir && substr($dir, -1) != "/")
$dir .= "/";


if ($directory_handle = opendir($dir)) {
while(($file_handle = readdir($directory_handle)) !== false) {
if (in_array($file_handle, $ignore)) {
} else if (is_dir($dir . $file_handle)) {
parseList($dir . $file_handle);
} else {
foreach (extensions as $extension) {
if (strpos($file_handle, $extension) !== false && subdirectory[$dir]) {
$old_name = $dir . $file_handle;
$new_name = $dir . str_replace(subdirectory[$dir]["from"], subdirectory[$dir]["to"], $file_handle);
copy($old_name, $new_name);
unlink($old_name);
}
}
reset(extensions);
}
}
}
}

?>
[/code]
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.