dcsimg
Results 1 to 3 of 3

Thread: [VBScript] Replacing text function value input on large txt file not work

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    1

    [VBScript] Replacing text function value input on large txt file not work

    Hi all, I need your appreciated help.

    The expiration.txt file contains 197.015 lines.

    Code:
    foo1; 2020-03-01 13:33;
    foo2; 2020-02-01 08:45;
    foo3; 2020-01-01 11:30;
    ...
    ...
    ...
    On this large txt file I need replaced the all date value from:

    1. 2020-03-01 13:33 to 2020-03-01
    2. 2020-02-01 08:45 to 2020-02-01
    3. 2020-01-01 11:30 to 2020-01-01
    4. ...
    5. ...
    6. 2018-01-01 12:40 to 2018-01-01 ( this is the last line number 197.015 )


    I have tried the code below.

    Don't have error but the replace input in txt file not work.

    The new expiration.txt file remained unchanged.

    How to do resolve this ?

    Thank you in advance for any help.

    Vince

    Code:
    Const ForReading = 1
    Const ForWriting = 2
    intCount = 0
    intIndex = 1
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    str_input = ""
    Set oInFile = oFSO.OpenTextFile("expiration.txt", 1)
    str_input = oInFile.ReadAll()
    Set oRegEx = CreateObject("VBScript.RegExp")
    With oRegEx
        .Multiline = True
        .Global = True
        .Pattern = "(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+);"
    End With
    Do Until oInFile.AtEndOfStream
    str_input = oInFile.ReadLine
    If (intCount = 0) Then
       str_input = oRegEx.Replace(str_input, "$1-$2-$3;")
       Set oInFile = oFSO.OpenTextFile("expiration.txt", 2)
       oInFile.Write str_input
       oInFile.Close
    End If
    intCount = intCount + 1
    If (intCount = 200) Then
        intCount = 0
        intIndex = intIndex + 1
        oInFile.Close
    End If
    Loop
    oInFile.Close
    set oFSO = nothing
    WScript.echo "ok"
    Last edited by UncleVince; Mar 28th, 2020 at 02:41 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,910

    Re: [VBScript] Replacing text function value input on large txt file not work

    Not sure, whether a new File shall be created for the new output, or not -
    but here's one version how to do that with Line-wise reading/writing on two Streams:

    Code:
      With CreateObject("Scripting.FileSystemObject")
        Set InpFile = .OpenTextFile("c:\temp\TestInp.log", 1)
        Set OutFile = .OpenTextFile("c:\temp\TestOut.log", 2, True)
        
        Do Until InpFile.AtEndOfStream
           Line = InpFile.ReadLine
    
           Pos = InStrRev(Line, "-")
           If Pos Then Line = Left(Line, Pos + 2) & ";"
    
           OutFile.WriteLine Line
        Loop
        
        InpFile.Close
        OutFile.Close
      End With
    And in case this is intended to be a replacement of the InputFiles-Content,
    one could do it this way, in a slightly faster manner (now with Variable-Declarations):
    Code:
      With CreateObject("Scripting.FileSystemObject")
        Dim FName, i, Lines, Pos
            FName = "c:\temp\TestInp.log"
        Lines = Split(Replace(.OpenTextFile(FName, 1).ReadAll, vbCr, ""), vbLf)
          For i = 0 To UBound(Lines)
              Pos = InStrRev(Lines(i), "-")
              If Pos Then Lines(i) = Left(Lines(i), Pos + 2) & ";"
          Next
        .OpenTextFile(FName, 2, True).Write Join(Lines, vbCrLf)
      End With
    HTH

    Olaf

  3. #3
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,994

    Re: [VBScript] Replacing text function value input on large txt file not work

    Quote Originally Posted by UncleVince View Post
    The new expiration.txt file remained unchanged.

    How to do resolve this ?
    Check out your regexp -- it's looking for time part including seconds -- "(\d+):(\d+):(\d+)".

    You don't need hours/minutes capturing groups (not referenced in the replacement expr with $1, $2, etc.) and you can mark seconds as an *optional * non-capturing group like this -- "\d+:\d+(?:\d+)?"

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width