问题
I have this VBScript mainly for replace function for multiple text files.
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 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
But this kind of code is taking too long (since its a specific filename based) and repetition of the same replace function, I would like to ask for a help on how to make replace function loops for all the files in the same folder.
A big appreciation for the answer.
回答1:
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("C:\windows")
Set Fls = fldr.files
For Each thing in Fls
msgbox thing.path
Next
also set contents = thing.readall.
来源:https://stackoverflow.com/questions/38448894/how-to-make-replace-function-loops-for-all-the-files-in-the-same-folder-same-di