满足条件时读写文本文件
Reading and Writing text file when condition are meet
我有一个文本文件 Sample.txt,其中包含数千条记录。
见下文。我已经创建了接受截止记录输入框的 vbscript。在这个例子中,我输入了 10005 作为截止记录。
然后脚本将读取截止记录之后的所有记录,在这种情况下从 10006 开始到 10010,然后写入一个新的文本文件,文件名为 20180920.txt
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
预期输出:
10006
10007
10008
10009
10010
示例脚本但尚未完成新文件的写入。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell = CreateObject("WScript.Shell")
Set oExec = wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
CurrentDate = Replace(Date, "/", "")
Lastrecord = InputBox("Last Last Record:")
IsFound = 0
IsFound1 = 0
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If Trim(strLine) = Trim(LastRecord) Then
IsFound = 1
Exit Do
End If
Loop
objFile1.Close
WScript.Echo strLine
WScript.Echo Trim(Lastrecord)
--------------------我已经完成了脚本,请参阅下面对@Ansgar Wiechers 的感谢。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
CurrentDate = Year(NOW) & Right("00" & Month(NOW), 2) & Right("00" & Day(NOW), 2) & Right("00" & Hour(NOW), 2) & Right("00" & Minute(NOW), 2) & Right("00" & Second(NOW), 2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
Lastrecord = InputBox("Last Last Record:")
IsFound = False
Set outFile = objFSO.CreateTextFile(objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT", True)
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If IsFound Then outFile.WriteLine strLine
If Trim(strLine) = Trim(LastRecord) Then IsFound = True
Loop
Wscript.Echo "New file created successfully at: " & objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT"
outFile.Close
objFile1.Close
找到截止线后,不要退出循环,而是将 strLine
写入输出文件。
IsFound = False
Set outFile = objFSO.OpenTextFile("C:\output.txt", 2)
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If IsFound Then outFile.WriteLine strLine
If Trim(strLine) = Trim(LastRecord) Then IsFound = True
Loop
outFile.Close
我有一个文本文件 Sample.txt,其中包含数千条记录。 见下文。我已经创建了接受截止记录输入框的 vbscript。在这个例子中,我输入了 10005 作为截止记录。 然后脚本将读取截止记录之后的所有记录,在这种情况下从 10006 开始到 10010,然后写入一个新的文本文件,文件名为 20180920.txt
10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010
预期输出:
10006 10007 10008 10009 10010
示例脚本但尚未完成新文件的写入。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell = CreateObject("WScript.Shell")
Set oExec = wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
CurrentDate = Replace(Date, "/", "")
Lastrecord = InputBox("Last Last Record:")
IsFound = 0
IsFound1 = 0
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If Trim(strLine) = Trim(LastRecord) Then
IsFound = 1
Exit Do
End If
Loop
objFile1.Close
WScript.Echo strLine
WScript.Echo Trim(Lastrecord)
--------------------我已经完成了脚本,请参阅下面对@Ansgar Wiechers 的感谢。
Dim Lastrecord
Dim IsFound, IsFound2
Dim CurrentDate
Const ForReading = 1
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
CurrentDate = Year(NOW) & Right("00" & Month(NOW), 2) & Right("00" & Day(NOW), 2) & Right("00" & Hour(NOW), 2) & Right("00" & Minute(NOW), 2) & Right("00" & Second(NOW), 2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(sFileSelected)
Set objFile1 = objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile), ForReading)
Lastrecord = InputBox("Last Last Record:")
IsFound = False
Set outFile = objFSO.CreateTextFile(objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT", True)
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If IsFound Then outFile.WriteLine strLine
If Trim(strLine) = Trim(LastRecord) Then IsFound = True
Loop
Wscript.Echo "New file created successfully at: " & objFSO.GetParentFolderName(objFile) & "\" & CurrentDate & ".DAT"
outFile.Close
objFile1.Close
找到截止线后,不要退出循环,而是将 strLine
写入输出文件。
IsFound = False
Set outFile = objFSO.OpenTextFile("C:\output.txt", 2)
Do Until objFile1.AtEndOfStream
strLine = objFile1.ReadLine
If IsFound Then outFile.WriteLine strLine
If Trim(strLine) = Trim(LastRecord) Then IsFound = True
Loop
outFile.Close