- 註冊時間
- 2002-8-8
- 線上時間
- 3361 小時
- 閱讀權限
- 200
- 積分
- 1429
- 主題
- 104
- 精華
- 1
- 文章
- 1533
該用戶從未簽到 - 文章
- 1533
|
有一些網路來的檔案在硬碟裡面,每次手動改名改到很煩,手動又花時間,於是用了點時間作了一個 PHP script,看看大家有沒有可能有需要。
這個 script 會產生一個 filename.bat,你可以用 notepad 打開看看結果是否符合預期,如果是的話,用檔案總管雙擊 filename.bat 下去,漂漂亮亮的檔名就整理好了。
如有任何建議也很歡迎。
filename.cmd,需要修改路徑指向到你的 php.exe,另外要載入 iconv:
- "D:\apache\php-4.3.9\php.exe" -f filename.php
複製代碼
filename.php(請注意:內容需要轉為 utf8):
- <?php
- if ($handle = opendir('.')) {
- $mStr = "";
- $mAryMatch = array("[","]","album","verycd.com","專輯","(mp3)",".-.","..","...","....","_","(ape)","cd1","cd2","cd3","cd4","cd5","cd6");
- $mAryReplace = array("" ,"" ,"" ,"" ,"" ," - ","." ,"." ,"." ," ","ape" ,"d1" ,"d2" ,"d3" ,"d4" ,"d5" ,"d6");
- while (false !== ($file = readdir($handle))) {
- $file = iconv('big5','UTF-8//TRANSLIT',$file);
- $fileTemp = $file;
- $fileTemp2 = "";
- $fileTemp = strtolower($fileTemp);
- $fileTemp = str_replace($mAryMatch, $mAryReplace, $fileTemp);
- //$mAryTemp = explode(".", $fileTemp);
- //$mAryTemp = split("\.", $fileTemp);
- $mAryTemp = preg_split ("/[ ,.]+/", $fileTemp);
- for ($mI = 0 ; $mI < count($mAryTemp) ; $mI++) {
- // 附檔名前要加點
- if ($mI == count($mAryTemp) - 1) {
- $fileTemp2 .= ".";
- }
- // 附檔名不處理大小寫
- // 特殊字不處理大小寫
- $mAryLower = array("the", "of", "and");
- if ( ($mI == count($mAryTemp) - 1) || (in_array($mAryTemp[$mI], $mAryLower)) ) {
- $fileTemp2 .= $mAryTemp[$mI];
- // 遇到 ape 轉為大寫 APE
- } else if ($mAryTemp[$mI] == "ape") {
- $fileTemp2 .= strtoupper($mAryTemp[$mI]);
- // 其他的都字頭字母大寫
- } else {
- $fileTemp2 .= ucfirst($mAryTemp[$mI]);
- }
- // 還沒結束前要加空白隔開
- if ($mI != count($mAryTemp) - 1 && $mI != count($mAryTemp) - 2) {
- $fileTemp2 .= " ";
- }
- }
- if ($file != "." && $file != ".." ) {
- $mStr .= "rename \"$file\" \"$fileTemp2\"\r\n";
- }
- // utf8 環境下無法執行以下兩行指令
- //exec("rename \"$file\" \"$fileTemp2\"");
- //rename($file, $fileTemp2);
- }
- $mStr = iconv('UTF-8','big5//TRANSLIT',$mStr);
- $mFP = fopen("./filename.bat", "w");
- fwrite($mFP, $mStr);
- closedir($handle);
- }
- ?>
複製代碼
[ Last edited by Dragoon on 2004-12-14 at 16:34 ] |
|