-2

I have a text file Sample.txt with that contains thousands of records. See below. I have created vbscript that accept input box for the cut-off record. In this example I inputted 10005 as the cut-off record. The script will then read all records after the cut-off record, in this case starting at 10006 down to 10010, then write to a new text file with the current date as file name like 20180920.txt

10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010

Expected output:

10006
10007
10008
10009
10010

Sample script but not finished in writing the new file.

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)

-------------------I have completed the script, see below credit to @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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Eliseo Jr
  • 141
  • 3
  • 15
  • Where is the script? Without it, its hard to tell how to fix your code – Pankaj Jaju Sep 20 '18 at 12:24
  • @PankajJaju , see above I append my initial script but writing to a new text file, I have not done it yet. – Eliseo Jr Sep 20 '18 at 12:42
  • I am assuming there won't be any duplication in row counters (10005, 10005, etc)? Can you confirm? – Pankaj Jaju Sep 20 '18 at 12:50
  • @pankajJaju , there is possibilities but when it found the first hit no matter if it has duplicate it wil just get all record below and write it in different txt file. – Eliseo Jr Sep 20 '18 at 17:49
  • Re-opened, b/c the alleged duplicate is about how to read/write files in general while the OP's question is about how to selectively write data from one file to another file. – Ansgar Wiechers Sep 21 '18 at 08:14

1 Answers1

1

Instead of exiting from the loop write strLine to the output file after the cutoff line was found.

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328