Mega Code Archive

 
Categories / Php / File Directory
 

Batch Uppercase-lowercase files and directories for a given path

Some people working on windows have encountered this problem, where all files and directories are all uppercase. While your website could run on windows, it couldn't be the case for *nix. So this script will help to batch rename files and directories to lowercase or uppercase. ------ Script start here ------ #!/usr/bin/php <?php $argv = $GLOBALS[HTTP_SERVER_VARS][argv]; $argc = $GLOBALS[HTTP_SERVER_VARS][argc]; if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) { ?> Batch Uppercase/lowercase files and directories for a given path. Usage : <?php echo $argv[0]; ?> <path> <upper/lower> <p> Sample (lowercase all files and directories in current folder): <?php echo $argv[0]; ?> <path> Path to your files/directories (default is current) <upper/lower> Lowercase or uppercase (default is lowercase) <p> print processed files/directories in the output Options --help, -help, -h, and -?, Will return this help <?php /** * @return void * @param array $base_dir * @desc return recursive list of files and directories */ function recrusive_dirlist($base_dir) { global $getDirList_alldirs,$getDirList_allfiles; function getDirList($base) { global $getDirList_alldirs,$getDirList_allfiles; if(is_dir($base)) { $dh = opendir($base); while (false !== ($dir = readdir($dh))) { if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..') //note the change in this line { // $subs = $dir ; $subbase = $base ."/". $dir;//note the change in this line $getDirList_alldirs[]=$subbase; getDirList($subbase); } elseif(is_file($base ."/". $dir) && $dir !== '.' && $dir !== '..')//change in this line too { $getDirList_allfiles[]=$base ."/". $dir;//change in this line too } } closedir($dh); } } getDirList($base_dir); $retval['dirs']=$getDirList_alldirs; $retval['files']=$getDirList_allfiles; return $retval; } /** * @return void * @param string $path * @param string $type * @param char $p * @desc process to batch rename with uppercase or lowercase */ function batch_ul($path,$type,$p){ // retrives array of files and directories if (trim($path)=="") { $curr = getcwd(); // Current dir } else $curr = $path; // user path $data = recrusive_dirlist($curr); // rename files foreach ($data["files"] as $in) { // lowercase just the last part $newname = explode("/",$in); switch($type){ case "upper": $newname[sizeof($newname)-1] = strtoupper($newname[sizeof($newname)-1]); break; default: $newname[sizeof($newname)-1] = strtolower($newname[sizeof($newname)-1]); break; } $lcname = "/".implode("/",$newname); shell_exec("mv $in ".$lcname); if (trim($p)!="") { echo "Copy done from $in to $lcname\n\r"; } } // then rename directories $data["dirs"] = array_reverse($data["dirs"]); foreach ($data["dirs"] as $in) { // lowercase just the last part $newname = explode("/",$in); switch($type){ case "upper": $newname[sizeof($newname)-1] = strtoupper($newname[sizeof($newname)-1]); break; default: $newname[sizeof($newname)-1] = strtolower($newname[sizeof($newname)-1]); break; } $lcname = "/".implode("/",$newname); shell_exec("mv $in ".$lcname); if (trim($p)!="") { echo "Copy done from $in to $lcname\n\r"; } } } $path = $argv[1]; $type = $argv[2]; $p = $argv[3]; batch_ul($path,$type,$p); ?>