-1

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.

Djamille
  • 71
  • 1
  • 1
  • 9
  • It's not going to take any less time processing by putting everything in a loop - it'll just be smaller code and easier to maintain – dbmitch Jul 19 '16 at 05:47
  • I realise you are fairly new but a lot of the questions you ask are duplicates, please consider a quick search in google *(use the [`site:stackoverflow.com` trick](https://www.google.com/search?q=read+files+in+a+folder+vbscript&oq=read+files+in+a+folder+vbscript&aqs=chrome..69i57j0l3.5935j0j7&sourceid=chrome&ie=UTF-8#safe=off&q=site:stackoverflow.com+read+files+in+a+folder+vbscript) to search only [so])* before posting a question to avoid disappointment. See [ask]. – user692942 Jul 19 '16 at 08:53

1 Answers1

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

also set contents = thing.readall.