#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    12
    Rep Power
    0

    Need help reading and writing text file using vbscript


    hi everyone!

    i had a vbscript that will read the text file from the bottom to the top, and after reading the file, it will be displayed in the screen, and the code look like this:
    PHP Code:
    Dim arrFileLines()
    0
    Set objFSO 
    CreateObject("Scripting.FileSystemObject")
    Set objFile objFSO.OpenTextFile("C:\script\test.txt"1)
    Do 
    Until objFile.AtEndOfStream
     Redim Preserve arrFileLines
    (i)
     
    arrFileLines(i) = objFile.ReadLine
     i 
    1
    Loop
    objFile
    .Close
    For Ubound(arrFileLinesto LBound(arrFileLinesStep -1
     Wscript
    .Echo arrFileLines(l)
    Next 
    and the contents of C:\script\test.txt look like this:
    PHP Code:
    5/19/2006   Success
    5
    /20/2006   Failure
    5
    /21/2006   Failure
    5
    /22/2006   Failure
    5
    /23/2006   Success 
    and the following information will be echoed to the screen:
    PHP Code:
    5/23/2006   Success
    5
    /22/2006   Failure
    5
    /21/2006   Failure
    5
    /20/2006   Failure
    5
    /19/2006   Success 
    instead, of displaying it in the screen, i want the script to write it to a text file, and save the text file in the same directory as C:\script\result.txt

    any help on this would be highly appreciated. thanks a LOT!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,879
    Rep Power
    24
    For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    Wscript.Echo arrFileLines(l)
    Next
    Use FSO to open a new file for writing.
    Replace the echo/response with the writeline function.

    http://www.w3schools.com/asp/met_writeline.asp
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    12
    Rep Power
    0
    thanks a lot degsy, i got it work now by using your code and the reference in http://www.w3schools.com/asp/met_writeline.asp.
    here's the code, maybe someone might need it too.
    PHP Code:
    Dim arrFileLines()
    0
    Set objFSO 
    CreateObject("Scripting.FileSystemObject")
    Set objFile objFSO.OpenTextFile("d:\backup\test.txt"1)

    ' create the output file here
    Set objoutputFile = objFSO.CreateTextFile("d:\backup\result.txt")

    Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
    Loop
    objFile.Close
    For l = Ubound(arrFileLines)-5 to Ubound(arrFileLines)

    write the output to the file
    objoutputFile
    .writeline arrFileLines(l)
    Next

    'Close the output file
    objoutputFile.close 

IMN logo majestic logo threadwatch logo seochat tools logo