如何对同一文件夹(同一目录)中的所有文件进行替换功能循环? [英] How to make replace function loops for all the files in the same folder (same 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 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
但是这种代码花费的时间太长(由于其基于特定的文件名)并且重复相同的替换函数,我想寻求有关如何为所有文件进行替换函数循环的帮助在同一文件夹中.
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.
非常感谢您的回答.
推荐答案
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
.
这篇关于如何对同一文件夹(同一目录)中的所有文件进行替换功能循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文