|
批量修改文件
读取一个文件夹,然后在修改里面的个个文件,用过正则表达的替换,
但是FileFindNextFile,只有文件名,不是绝对路径。
求各位大神帮助!!
谢谢!!
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 409, 148, 192, 114)
GUICtrlCreateLabel("FilePath", 8, 16, 53, 23)
GUICtrlSetFont(-1, 12, 400, 0, "Times New Roman")
$filepath = GUICtrlCreateInput("", 64, 16, 281, 21)
$Input3 = GUICtrlCreateInput("Input3", 96, 96, 105, 21)
$Input4 = GUICtrlCreateInput("Input4", 264, 96, 105, 21)
$SearchString = GUICtrlCreateLabel("SearchString", 112, 72, 65, 17)
$ReplaceString = GUICtrlCreateLabel("ReplaceString", 280, 72, 71, 17)
$rep = GUICtrlCreateButton("replace", 16, 96, 43, 25)
$browser = GUICtrlCreateButton("...", 352, 16, 20, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $browser
$folderpath = FileSelectFolder ("Choose build path ...","")
GUICtrlSetData($filepath, $folderpath)
Case $rep
$searchstr = GUICtrlRead($Input3)
$replacestr = GUICtrlRead($Input4)
$filepath = GUICtrlRead($filepath)
Local $search = FileFindFirstFile($filepath & "\*.*")
; Check if the search was successful
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
Exit
EndIf
While 1
Local $file = FileFindNextFile($search)
MsgBox(0,"File Name",$file)
If @error Then ExitLoop
_replace($file,$searchstr,$replacestr)
WEnd
; Close the search handle
FileClose($search)
MsgBox(0,"","finish !!")
EndSwitch
WEnd
Func _replace($path,$search,$replace)
$text = ""
$readfile = FileOpen($path, 0)
If $readfile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
While 1
$text = $text & FileReadLine($readfile) & @CRLF
If @error = -1 Then ExitLoop
WEnd
FileClose($readfile)
$text = StringReplace ( $text, $search,$replace )
$writefile = FileOpen($path, 2)
FileWriteLine($writefile, $text)
FileClose($writefile)
MsgBox(0,"","Finish !")
EndFunc |
|