Text 在特定文本后读取,当到达空行时停止读取,然后将其写入输出文件
目前我只能读写从输入文件到输出文件的所有内容。但我只想读取book1 | book2 | book3之后的行,并在到达空行时停止读取,然后根据读取的内容写入输出文件。我已经找到了这个代码Text 在特定文本后读取,当到达空行时停止读取,然后将其写入输出文件,text,vbscript,Text,Vbscript,目前我只能读写从输入文件到输出文件的所有内容。但我只想读取book1 | book2 | book3之后的行,并在到达空行时停止读取,然后根据读取的内容写入输出文件。我已经找到了这个代码nStr(strLine,sStringToFind),但是我不知道它在这里是否能正常工作,或者如何使用它 我的输入文件 基于我的代码的我的当前输出 这是我想要实现的输出 将部分输入文件复制到输出文件-顶级结构: 准备工作 读/写循环 清理 准备:打开文件并设置状态变量以指示写入 设置tsIn=oFS.Op
nStr(strLine,sStringToFind)
,但是我不知道它在这里是否能正常工作,或者如何使用它
我的输入文件
基于我的代码的我的当前输出
这是我想要实现的输出
将部分输入文件复制到输出文件-顶级结构:
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
... Kernel ...
Loop
有趣的事件
If sLine = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If sLine = "" Then
Exit Do
Else
tsOut.WriteLine sLine
End If
End If
End If
更新wrt@Hackoo的答案:
A上述计划的适当实施:
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn : Set tsIn = oFS.OpenTextFile("..\data\25301708.txt")
Dim tsOut : Set tsOut = oFS.CreateTextFile("..\data\25301708-out.txt")
Dim bWrite : bWrite = False
Dim sLine
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
If sLine = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If sLine = "" Then
Exit Do
Else
tsOut.WriteLine sLine
End If
End If
End If
Loop
tsOut.Close
tsIn.Close
将部分输入文件复制到输出文件-顶级结构:
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
... Kernel ...
Loop
有趣的事件
If sLine = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If sLine = "" Then
Exit Do
Else
tsOut.WriteLine sLine
End If
End If
End If
更新wrt@Hackoo的答案:
A上述计划的适当实施:
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn : Set tsIn = oFS.OpenTextFile("..\data\25301708.txt")
Dim tsOut : Set tsOut = oFS.CreateTextFile("..\data\25301708-out.txt")
Dim bWrite : bWrite = False
Dim sLine
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
If sLine = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If sLine = "" Then
Exit Do
Else
tsOut.WriteLine sLine
End If
End If
End If
Loop
tsOut.Close
tsIn.Close
请尝试以下代码:
Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Result = Between_Ands(Data,"book1|book2|book3","fax1|fax2|fax3")
Lines = Split(Result,vbCrLf)
For Each Line In Lines
If Len(Line) > 0 Then
WriteLog Line,LogFile
end if
Next
ws.Run LogFile,1,False
'**********************************************************************************************
Function Between_Ands(ByVal Full_String, ByVal First_Delimiter, ByVal Second_Delimiter)
Dim Pos,Pos2
Pos = InStr(Full_String, First_Delimiter)
Pos2 = InStr(Full_String, Second_Delimiter)
If Pos = 0 Or Pos2 = 0 Then
Between_Ands = "Missing Delimiter"
Exit Function
End If
Between_Ands = Mid(Full_String, Pos + Len(First_Delimiter), Pos2 - (Pos + Len(First_Delimiter)))
End Function
'***********************************************************************************************
Function ReadFileText(sFile)
Dim objFSO,oTS,sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(sFile) Then
MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
Wscript.Quit
Else
Set oTS = objFSO.OpenTextFile(sFile)
sText = oTS.ReadAll
oTS.close
set oTS = nothing
Set objFSO = nothing
ReadFileText = sText
End if
End Function
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'************************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
使用由Ekkehard.Horner提出的解决方案
你必须这样做:
Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line,bWrite
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
bWrite = False
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Lines = Split(Data,vbCrLf)
For Each Line In Lines
If Line = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If Line = "" Then
Exit For
Else
WriteLog Line,LogFile
End If
End if
End if
Next
ws.Run LogFile,1,False
'***********************************************************************************************
Function ReadFileText(sFile)
Dim objFSO,oTS,sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(sFile) Then
MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
Wscript.Quit
Else
Set oTS = objFSO.OpenTextFile(sFile)
sText = oTS.ReadAll
oTS.close
set oTS = nothing
Set objFSO = nothing
ReadFileText = sText
End if
End Function
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'************************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
请尝试以下代码:
Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Result = Between_Ands(Data,"book1|book2|book3","fax1|fax2|fax3")
Lines = Split(Result,vbCrLf)
For Each Line In Lines
If Len(Line) > 0 Then
WriteLog Line,LogFile
end if
Next
ws.Run LogFile,1,False
'**********************************************************************************************
Function Between_Ands(ByVal Full_String, ByVal First_Delimiter, ByVal Second_Delimiter)
Dim Pos,Pos2
Pos = InStr(Full_String, First_Delimiter)
Pos2 = InStr(Full_String, Second_Delimiter)
If Pos = 0 Or Pos2 = 0 Then
Between_Ands = "Missing Delimiter"
Exit Function
End If
Between_Ands = Mid(Full_String, Pos + Len(First_Delimiter), Pos2 - (Pos + Len(First_Delimiter)))
End Function
'***********************************************************************************************
Function ReadFileText(sFile)
Dim objFSO,oTS,sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(sFile) Then
MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
Wscript.Quit
Else
Set oTS = objFSO.OpenTextFile(sFile)
sText = oTS.ReadAll
oTS.close
set oTS = nothing
Set objFSO = nothing
ReadFileText = sText
End if
End Function
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'************************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
使用由Ekkehard.Horner提出的解决方案
你必须这样做:
Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line,bWrite
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
bWrite = False
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Lines = Split(Data,vbCrLf)
For Each Line In Lines
If Line = "book1|book2|book3" Then
bWrite = True
Else
If bWrite Then
If Line = "" Then
Exit For
Else
WriteLog Line,LogFile
End If
End if
End if
Next
ws.Run LogFile,1,False
'***********************************************************************************************
Function ReadFileText(sFile)
Dim objFSO,oTS,sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(sFile) Then
MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
Wscript.Quit
Else
Set oTS = objFSO.OpenTextFile(sFile)
sText = oTS.ReadAll
oTS.close
set oTS = nothing
Set objFSO = nothing
ReadFileText = sText
End if
End Function
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'************************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
我发现自己今晚需要一个类似的剧本
Instr
在搜索字符串时就像布尔值一样,即使它从技术上为您提供了匹配的位置(在您正在搜索的另一个字符串中)。我发现它也有点灵活(比直接与相等者比较)<代码>否则,如果合并了主逻辑:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strInFile = "inFile.txt" 'Put a full path if you want
strOutFile = "outFile.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFS.OpenTextFile(strInFile, ForReading)
Set objOutFile = objFS.OpenTextFile(strOutFile, ForWriting, True)
writeNow = false
Do Until objInFile.AtEndOfStream
sReadLine = objInFile.ReadLine()
If instr(sReadLine, "book1|book2|book3") Then
writeNow = true 'We exit the if statement here ready to write on the next loop
ElseIf (writeNow AND Trim(sReadLine) = "") Then
Exit Do 'We exit the loop, finished writing
ElseIf writeNow Then
objOutFile.WriteLine sReadLine 'Otherwise, we write
End If
Loop
objOutFile.Close
objInFile.Close
VBScript万岁 我发现自己今晚需要一个类似的剧本
Instr
在搜索字符串时就像布尔值一样,即使它从技术上为您提供了匹配的位置(在您正在搜索的另一个字符串中)。我发现它也有点灵活(比直接与相等者比较)<代码>否则,如果合并了主逻辑:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strInFile = "inFile.txt" 'Put a full path if you want
strOutFile = "outFile.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFS.OpenTextFile(strInFile, ForReading)
Set objOutFile = objFS.OpenTextFile(strOutFile, ForWriting, True)
writeNow = false
Do Until objInFile.AtEndOfStream
sReadLine = objInFile.ReadLine()
If instr(sReadLine, "book1|book2|book3") Then
writeNow = true 'We exit the if statement here ready to write on the next loop
ElseIf (writeNow AND Trim(sReadLine) = "") Then
Exit Do 'We exit the loop, finished writing
ElseIf writeNow Then
objOutFile.WriteLine sReadLine 'Otherwise, we write
End If
Loop
objOutFile.Close
objInFile.Close
VBScript万岁