4.06.2008

autoit-更改檔名中的時間

這次的起因是因為,公司的工時表是用excel
每天、每人會有一個當天的呈報表,檔名有時間
若是一個人要管的時候,每天要花不少時間去改
所以就寫了這一個範本
當然,後來還要加上加班內容的更改
(總不能每天都一樣嘛!~ 老闆要看樣子就做做樣子給他看:))
內容我想就自由發揮吧!
本篇做個拋磚引玉囉

這一次要用到一個UDF的函式
在autoit中寫在Date.au3中
所以要引用其中的函式需要加上
#include 以使用時間相關的函式
對於程式的解說這次就直接穿插在程式中


;批次改檔
#include ;引入UDF函式

; 找出同資料夾內的所有excel檔案
$search = FileFindFirstFile("*.xls")

; 確認search是否成功
If $search = -1 Then
MsgBox(0, "Error", "找不到相符的檔名&資料夾")
Exit
EndIf

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

;取時間
$today = _NowCalcDate() ;取得的日期格式為 YYYY/MM/DD
$yestoday = _DateAdd( 'd', -1, $today)
$today = StringMid(StringReplace($today, "/", ""), 3)
$yestoday = StringMid(StringReplace($yestoday, "/", ""), 3)
;字串取代
$new_Name = StringReplace($file, $yestoday, $today)

;更名
FileMove($file, $new_name)

MsgBox(4096, "File:", $file & "-->" & $new_name)
WEnd

; Close the search handle
FileClose($search)

沒有留言: