如何为同一文件夹(同一目录)中的所有文件进行替换功能循环?

时间:2016-07-19 03:00:17

标签: loops replace vbscript directory

我有这个VBScript主要用于替换多个文本文件的功能。

Const ForReading = 1
Const ForWriting = 2

'''''''''''''''''''''''''''''''''''''''''''''''''' START OF 1st REPORTS ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\rep01_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForReading)

IF objFSO.GetFile("D:\rep01_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt").size <> 0 then    
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "$$$970A1", "SSS970A1",1,1)
strNewText1 = Replace(strNewText, "$$$979A1", "SSS979A1",1,1)
strNewText2 = Replace(strNewText1, "$$$983A1", "SSS983A1",1,1)

Set objFile = objFSO.OpenTextFile("D:\rep01_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForWriting)
objFile.WriteLine strNewText2 

objFile.Close
END IF
'''''''''''''''''''''''''''''''''''''''''''''''''' END OF 1st REPORTS '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' START OF 2nd REPORTS '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchAP_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForReading)

IF objFSO.GetFile("D:\rep01_UnmatchAP_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt").size <> 0 then    
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "$$$970A1", "SSS970A1",1,1)
strNewText1 = Replace(strNewText, "$$$979A1", "SSS979A1",1,1)
strNewText2 = Replace(strNewText1, "$$$983A1", "SSS983A1",1,1)

Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchAP_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForWriting)
objFile.WriteLine strNewText2 

objFile.Close
END IF
'''''''''''''''''''''''''''''''''''''''''''''''''' END OF 2nd REPORTS ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' START OF 3rd REPORTS  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchAR_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForReading)

IF objFSO.GetFile("D:\rep01_UnmatchAR_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt").size <> 0 then    
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "$$$970A1", "SSS970A1",1,1)
strNewText1 = Replace(strNewText, "$$$979A1", "SSS979A1",1,1)
strNewText2 = Replace(strNewText1, "$$$983A1", "SSS983A1",1,1)

Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchAR_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForWriting)
objFile.WriteLine strNewText2 

objFile.Close
END IF
'''''''''''''''''''''''''''''''''''''''''''''''''' END OF 3rd REPORTS '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' START OF 4th REPORTS ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchCutOff_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForReading)

IF objFSO.GetFile("D:\rep01_UnmatchCutOff_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt").size <> 0 then    
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "$$$970A1", "SSS970A1",1,1)
strNewText1 = Replace(strNewText, "$$$979A1", "SSS979A1",1,1)
strNewText2 = Replace(strNewText1, "$$$983A1", "SSS983A1",1,1)

Set objFile = objFSO.OpenTextFile("D:\rep01_UnmatchCutOff_" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", ForWriting)
objFile.WriteLine strNewText2 

objFile.Close
END IF
'''''''''''''''''''''''''''''''''''''''''''''''''' END OF 4th REPORTS '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

但是这种代码花了太长时间(因为它是基于特定的文件名)和重复相同的替换函数,我想请求帮助如何为所有文件进行替换函数循环在同一文件夹

非常感谢答案。

1 个答案:

答案 0 :(得分:0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("C:\windows")

Set Fls = fldr.files

For Each thing in Fls
    msgbox thing.path
Next

set contents = thing.readall