找回密码
 加入
搜索
查看: 2844|回复: 1

[AU3基础] 批量修改

[复制链接]
发表于 2014-3-3 14:59:45 | 显示全部楼层 |阅读模式
批量修改文件
读取一个文件夹,然后在修改里面的个个文件,用过正则表达的替换,
但是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
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-16 23:30 , Processed in 0.070690 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表