删除匹配字符串之间的多行
delete multiple lines between matched strings
我正在编写一个脚本来删除文件中两个匹配字符串之间的多行。
代码如下,执行后删除整行。谁能建议我如何实现这个?
Const ForReading = 1
Const ForWriting = 2
count = 0
strFileName = Wscript.Arguments(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
flag = 0
If InStr(strLine, "BBB") = 0 Then
flag = 1
End If
If flag = 1 Then
Exit Do
End If
If count = 1 Then
If flag = 0 Then
'strNewContents = strNewContents & strLine & vbCrLf
End If
End If
If InStr(strLine, "GGG") = 0 Then
strLine = ""
'strNewContents = strNewContents & strLine & vbCrLf
count = 1
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewContents
objFile.Close
}
文件包含如下
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
III
JJJ
我期望输出为
AAA
HHH
III
JJJ
以下行会导致您的脚本在遇到包含 "BBB" 的 not 行时立即退出循环,留下一个空变量 [=12] =]:
If InStr(strLine, "BBB") = 0 Then
flag = 1
End If
If flag = 1 Then
Exit Do
End If
您真正想要做的是在循环外初始化标志变量,并在遇到符合条件的字符串时切换它:
skip = False
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, "BBB") > 0 Then skip = True
If Not skip Then
If IsEmpty(strNewContents) Then
strNewContents = strLine
Else
strNewContents = strNewContents & vbNewLine & strLine
End If
End If
If InStr(strLine, "GGG") > 0 Then skip = False
Loop
我正在编写一个脚本来删除文件中两个匹配字符串之间的多行。
代码如下,执行后删除整行。谁能建议我如何实现这个?
Const ForReading = 1
Const ForWriting = 2
count = 0
strFileName = Wscript.Arguments(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
flag = 0
If InStr(strLine, "BBB") = 0 Then
flag = 1
End If
If flag = 1 Then
Exit Do
End If
If count = 1 Then
If flag = 0 Then
'strNewContents = strNewContents & strLine & vbCrLf
End If
End If
If InStr(strLine, "GGG") = 0 Then
strLine = ""
'strNewContents = strNewContents & strLine & vbCrLf
count = 1
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewContents
objFile.Close
}
文件包含如下
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
III
JJJ
我期望输出为
AAA
HHH
III
JJJ
以下行会导致您的脚本在遇到包含 "BBB" 的 not 行时立即退出循环,留下一个空变量 [=12] =]:
If InStr(strLine, "BBB") = 0 Then
flag = 1
End If
If flag = 1 Then
Exit Do
End If
您真正想要做的是在循环外初始化标志变量,并在遇到符合条件的字符串时切换它:
skip = False
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, "BBB") > 0 Then skip = True
If Not skip Then
If IsEmpty(strNewContents) Then
strNewContents = strLine
Else
strNewContents = strNewContents & vbNewLine & strLine
End If
End If
If InStr(strLine, "GGG") > 0 Then skip = False
Loop