Jump to content


Photo

Renama files under stubdirectory's..


  • Please log in to reply
3 replies to this topic

#1 xam

xam
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 13 October 2006 - 02:22 PM

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/specialtag_filename1.psd
to
d:/ftp/directory2/subdirectory/subdirectory2/xam_filename1.psd

or

d:/ftp/directory2/subdirectory2/subdirectory3/specialtag_filename2.jpg
to
d:/ftp/directory2/subdirectory2/subdirectory3/xam_filename2.jpg

is this possible with php?

regards,
xam.


#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 13 October 2006 - 02:31 PM

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.

<?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);
			}
		}
	}
}

?>

SA PHP Archives - www.phparchives.za.org

#3 xam

xam
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 13 October 2006 - 03:17 PM

thank u but there are over 300 subdirectorys and 500 files with different extentions.. i dont want write these names myself.. :) its possible to find subdirs + files = changa names only?

#4 xam

xam
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 14 October 2006 - 11:21 AM

:(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users