我想从文本文件中删除3行,从名称test,test2,test3开始。 我创建的以下脚本只删除一个,如何将其设置为删除3行?
Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "c:\test.txt"
strCheckForString = UCase("Directory")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
For Each strLine In arrLines
If Not(Left(UCase(LTrim(strLine)),Len(strCheckForString)) = strCheckForString) Then
objTS.WriteLine strLine
End If
Next
答案 0 :(得分:0)
这里有一个字符串:
strCheckForString = UCase("Directory")
(顺便说一下,你可以这样编写strCheckForString = "DIRECTORY"
)
您可以添加其他字符串
strCheckForString1 = "AnotherString"
strCheckForString2 = "ThirdString"
然后,在循环内部,你需要检查其他字符串是否匹配
If Not(Left(UCase(LTrim(strLine)),Len(strCheckForString)) = strCheckForString) AND
Not(Left(UCase(LTrim(strLine)),Len(strCheckForString1)) = strCheckForString1) AND
Not(Left(UCase(LTrim(strLine)),Len(strCheckForString2)) = strCheckForString2) Then
objTS.WriteLine strLine
End If
我会让你知道这实际上是做什么的: - )