Sub 批量修改文本內容()
Dim Fn$, MyPath$, MyFile$, NewName$, myText
Fn = Application.GetOpenFilename("請隨便選擇一個文件 (*.*), *.*", , "請隨便選擇一個文件,讓系統確定路徑:")
MyPath = Left(Fn, InStrRev(Fn, "\"))
MyFile = Dir(MyPath & "*.txt") ' 可以指明文件類型,也可以不指明。
Do While MyFile <> "" ' 開始循環。
Open MyFile For Input As #1 '打開第一個txt文件
Open MyFile & "temp" For Append As #2 '創建這個文件名+temp的臨時文件,并打開
Do While Not EOF(1) '循環至文件結尾
DoEvents '用于中斷,避免鼠標卡住
Line Input #1, myText '讀取這個文件的每一行
If InStr(myText, "元寶") > 0 Then '如果存在“元寶”這個關鍵字
Print #2, ";" & myText '在前面加分號
Else
Print #2, myText '否則輸出原來的內容
End If
Loop
Close #2
Close #1
Kill MyFile '刪除原文件
Name MyFile & "temp" As MyFile '重命名臨時文件為原文件
MyFile = Dir ' 查找下一個文件。
Loop
End Sub |