Vbscript 满足条件时读写文本文件

Vbscript 满足条件时读写文本文件,vbscript,Vbscript,我有一个包含数千条记录的文本文件Sample.txt。 见下文。我创建了vbscript,用于接受截止记录的输入框。在这个例子中,我输入了10005作为截止记录。 然后,脚本将读取截止记录之后的所有记录,在本例中,从10006到10010,然后写入一个新的文本文件,当前日期为文件名,如20180920.txt 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10000 10001 10002 10003 1000

我有一个包含数千条记录的文本文件Sample.txt。 见下文。我创建了vbscript,用于接受截止记录的输入框。在这个例子中,我输入了10005作为截止记录。 然后,脚本将读取截止记录之后的所有记录,在本例中,从10006到10010,然后写入一个新的文本文件,当前日期为文件名,如20180920.txt

10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 预期产出:

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)
Dim Lastrecord
Dim IsFound,IsFound2
当前日期
常数ForReading=1
设置wShell=CreateObject(“WScript.Shell”)
设置oExec=wShell.Exec(“mshta.exe”关于:FILE.click();新建ActiveXObject('Scripting.FileSystemObject')。GetStandardStream(1)。WriteLine(FILE.value);close();resizeTo(0,0);“”)
sFileSelected=oExec.StdOut.ReadLine
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFile=objFSO.GetFile(sFileSelected)
设置objFile1=objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile),用于读取)
CurrentDate=替换(日期“/”,“”)
Lastrecord=InputBox(“最后一条记录:”)
IsFound=0
IsFound1=0
直到objFile1.AtEndOfStream
strLine=objFile1.ReadLine
如果Trim(strLine)=Trim(LastRecord),则
IsFound=1
退出Do
如果结束
环
objFile1.Close
WScript.Echo strLine
WScript.Echo修剪(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
Dim Lastrecord
Dim IsFound,IsFound2
当前日期
常数ForReading=1
设置wShell=CreateObject(“WScript.Shell”)
设置oExec=wShell.Exec(“mshta.exe”关于:FILE.click();新建ActiveXObject('Scripting.FileSystemObject')。GetStandardStream(1)。WriteLine(FILE.value);close();resizeTo(0,0);“”)
sFileSelected=oExec.StdOut.ReadLine
当前日期=年(现在)和右(“00”和月(现在),2)和右(“00”和天(现在),2)和右(“00”和小时(现在),2)和右(“00”和分钟(现在),2)和右(“00”和秒(现在),2)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFile=objFSO.GetFile(sFileSelected)
设置objFile1=objFSO.OpenTextFile(objFSO.GetAbsolutePathName(objFile),用于读取)
Lastrecord=InputBox(“最后一条记录:”)
IsFound=False
Set outFile=objFSO.CreateTextFile(objFSO.GetParentFolderName(objFile)&“\”&CurrentDate&“.DAT”,True)
直到objFile1.AtEndOfStream
strLine=objFile1.ReadLine
如果找到,则为outFile.WriteLine strLine
如果Trim(strLine)=Trim(LastRecord),则IsFound=True
环
Wscript.Echo“在以下位置成功创建了新文件:”&objFSO.GetParentFolderName(objFile)&“\”&CurrentDate&“.DAT”
出口,关闭
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

剧本在哪里?没有它,很难告诉你如何修复你的问题code@PankajJaju,请参见上文我附加了我的初始脚本,但写入了一个新的文本文件,我还没有这样做。我假设行计数器中不会有任何重复(10005、10005等)?你能确认吗?@pankajJaju,有可能,但当它发现第一个命中时,无论是否有重复,都会得到下面的所有记录,并将其写入不同的txt文件。重新打开,b/c所谓的重复是关于如何读取/写入文件,而OP的问题是如何有选择地将数据从一个文件写入另一个文件。确切地说,这就是解决方案。