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
https://forums.phpfreaks.com/topic/23852-renama-files-under-stubdirectorys/
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.