满足条件时读写文本文件

时间:2018-09-20 12:17:21

标签: vbscript

我有一个文本文件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

1 个答案:

答案 0 :(得分:1)

找到截断线之后,不要从循环中退出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