3.28.2008

AutoIt - 自動批次更名

最近在工作上需要
要把以前在DOS時代時習慣的命名方式(8.3的命名方式)
改為新的名稱,但檔案又有上千個
嘿嘿~~ 這時~
autoIt又可以派上用場了
以下就是我寫的一個小程式
解說一下「它」做了什麼

  1. 相同類型的檔案有固定的開頭,所以用FileFindFirstFile找出需要的檔案,再對它更名
  2. FileFindNextFile從找到的檔案群中跳到下一個檔案
  3. 另外就是視自己所需囉,我為我需要在檔案中加入"-",所以才會有StringLeft和StringMid的運用
  4. 最後呢,StringReplace(string, a_string, b_string)是將string中的a_string改為b_string
  5. ok,我想這幾個指令掌握了就可以了解這個程式在做什麼了



;=============================
; rename
;=============================

;找出開頭為"xx*.*的檔案
$init_string = InputBox("請輸入開頭字母", "請輸入開頭字母")
$exchange_string = InputBox("請輸入要替代的字串", "請輸入要替代的字串")
$exchange_string = $exchange_string & "-"
$search = FileFindFirstFile($init_string & "*.*")

;確認search是否成功
If $search = -1 Then
MsgBox(0, "Error", "沒有相符合的字串或資料夾")
Exit
EndIf

While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop

$new_name = StringReplace($file, $init_string , $exchange_string)
$new_name = StringLeft($new_name, 6) & "-" & StringMid($new_name, 7)
;MsgBox(4096, "File:", "File_name: " & $file & chr(13) & "new_name: " & $new_name)
FileMove($file, $new_name)

WEnd

; Close the search handle
FileClose($search)

沒有留言: