Reading a txt-file with VBScript

Author Message
blackmoon

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 10/21/2004
  • Location: Belgium
  • Status: offline
Reading a txt-file with VBScript Thursday, October 21, 2004 9:58 PM (permalink)
0
Hello

Is it possible in one way or another to open a txt-file with vbscript, read the data in it and store it in variables?

It may sound as a "dumb" question ... but i'm new to vbscript.

Can anyone help me with this?

greetz Blackmoon

PS: if you know where I can find good tutorials / courses on VBScript, they are most welcome
 
#1
    nicksvbs

    • Total Posts : 26
    • Scores: 2
    • Reward points : 0
    • Joined: 6/28/2004
    • Location:
    • Status: offline
    Re: Reading a txt-file with VBScript Thursday, October 21, 2004 10:49 PM (permalink)
    0
    Two ways to do that: An array and with a dictionary object. Both of the following scripts open and display the lines of c:\boot.ini. As for information on scripting try this http://www.microsoft.com/resources/documentation/windows/2000/server/scriptguide/en-us/sas_vbs_dcot.mspx. It's very good. That's where I learned myself. Good luck learning.

    The array:
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("c:\boot.ini", ForReading)
    
    Const ForReading = 1
    
    Dim arrFileLines()
    i = 0
    Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
    Loop
    objFile.Close
    
    'Then you can iterate it like this
    
    For Each strLine in arrFileLines
    WScript.Echo strLine
    Next
    



    The dictionary object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDictionary = CreateObject("Scripting.Dictionary")
    
    Const ForReading = 1
    
    Set objFile = objFSO.OpenTextFile ("c:\boot.ini", ForReading)
    i = 0
    Do Until objFile.AtEndOfStream
    strNextLine = objFile.Readline
    If strNextLine <> "" Then
    objDictionary.Add i, strNextLine
    End If
    i = i + 1
    Loop
    objFile.Close
    
    'Then you can iterate it like this
    
    For Each strLine in objDictionary.Items
    WScript.Echo strLine
    Next
    
     
    #2
      blackmoon

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 10/21/2004
      • Location: Belgium
      • Status: offline
      Re: Reading a txt-file with VBScript Thursday, October 21, 2004 11:18 PM (permalink)
      0
      Ok ... thx man :-)

      Now i got another question ... is it possible to split a string?

      When i read the txt-file I get the following:

      DRAWINGS|ISOS|ISO-WN119-0002

      but now I would like to split this so that I get 3 different variables:
      var1: DRAWINGS
      var2: ISOS
      var3: ISO-WN119-0002

      I know how I can do this in PHP, but I have no clue of how to do it in VBScript

      any suggestions?

      greetz Blackmoon
       
      #3
        mbouchard

        • Total Posts : 2110
        • Scores: 29
        • Reward points : 0
        • Joined: 5/15/2003
        • Location: USA
        • Status: offline
        Re: Reading a txt-file with VBScript Friday, October 22, 2004 12:49 AM (permalink)
        0
        A couple good sources are the Windows Script help file found here http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en and The TechNet script center found here http://www.microsoft.com/technet/scriptcenter/default.mspx
        Example taken from documentation:

        quote:
        To split a line, use split
        MyString = "DRAWINGS|ISOS|ISO-WN119-0002"
        MyArray = Split(MyString, "|", -1, 1)
        Msgbox MyArray(0)
        Msgbox MyArray(1)
        Msgbox MyArray(2)

        Mike
         
        #4
          blackmoon

          • Total Posts : 5
          • Scores: 0
          • Reward points : 0
          • Joined: 10/21/2004
          • Location: Belgium
          • Status: offline
          Re: Reading a txt-file with VBScript Friday, October 22, 2004 12:58 AM (permalink)
          0
          woot woot

          thx, it works just great ... just the way I wanted it

          thx again

          greetz Blackmoon
           
          #5
            Xpyder

            • Total Posts : 1
            • Scores: 0
            • Reward points : 0
            • Joined: 6/11/2009
            • Status: offline
            RE: Re: Reading a txt-file with VBScript Thursday, June 11, 2009 11:06 AM (permalink)
            0
            I looked over this and both those code examples are not really good in actual implementation.

            A Dictionary list has no inherent order and should only really be used when using keywords and values or when trying to prevent duplicate entries(like when you take in parameters)

            A "redim preserve" is relatively expensive computationally, but the difference between adding 1 item and adding 50 items is insignificant.
            Therefore it's far better to allocate far more slots then you expect to use, and to use a "HighWaterMark" to keep track of the end of the actual array(vs the array and the rest of the buffered slots)

            option explicit
             
             Dim arrFileLines()
             parseFileToArray "c:\boot.ini", arrFileLines
             'echoArraySimple arrFileLines
             echoArrayNice arrFileLines
             wscript.quit
             
             '=====
             sub parseFileToArray(byVal pFilename, byRef pArray)
              'open - declare variables and stuff
              dim objFSO,objFile, hwmArray,x 'hwm = HighWaterMark
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              Set objFile = objFSO.OpenTextFile(pFilename, ForReading)
              CONST ForReading = 1
              CONST INIT = 100 'INIT = Inital Array Size
              CONST EXP = 250 'EXP = expand the array by this much if it fills up
              
              'main - do the work
              hwmArray = -1
              redim pArray(INIT)
              Do Until objFile.AtEndOfStream
               hwmArray = hwmArray + 1
               x = hwmArray
               if hwmArray >= ubound(pArray) then redim preserve pArray(hwmArray + EXP) 'if the array fills up, buffer EXP more entries
               pArray(x) = objFile.ReadLine
              Loop
              redim preserve pArray(hwmArray) 'shrink array to actual size
              
              'close - set the return value, close objects and stuff
              objFile.Close
              set objFile = nothing
              set objFSO = nothing
             end sub
             '=====
             sub echoArraySimple(byRef pArray)
              dim strLine
              For Each strLine in pArray
               WScript.Echo strLine
              Next
             end sub
             '=====
             sub echoArrayNice(byRef pArray)
              dim strLine,msg
              For Each strLine in pArray
               msg = msg & strLine & vbCRLF
              Next
              wscript.echo msg
             end sub
             


            there are 3 subs in this code,
            one reads in the file line by line and puts it into the array you pass it.
            one just runs through the array you pass it and echo's each item.
            one runs through the array, puts a "vbCRLF"(newline) after each of the entries, stores them in a "msg" variable and then echo's that all at once to make it look nice.

            to see the difference in output use WScript instead of CScript.
             
            #6
              Speaker2Animals

              • Total Posts : 1
              • Scores: 0
              • Reward points : 0
              • Joined: 4/7/2010
              • Status: offline
              RE: Re: Reading a txt-file with VBScript Wednesday, April 07, 2010 7:47 AM (permalink)
              0
              There's an even easier way to read a file than that:

              function readFile(sPath)
                  const forReading = 1
                  dim objFSO, objFile, sData
                  set objFSO = createobject("Scripting.FileSystemObject")
                  set objFile = objFSO.openTextFile(sPath, ForReading)
                  sData = ""
                  do until objFile.atEndOfStream
                      sData = sData & objFile.readLine & vbCrLf
                  loop
                  objFile.close
                  set objFile = nothing
                  set objFSO = nothing
                  readFile = sData
              end function
               
              #7
                danygug

                • Total Posts : 1
                • Scores: 0
                • Reward points : 0
                • Joined: 11/2/2010
                • Status: offline
                RE: Re: Reading a txt-file with VBScript Tuesday, November 02, 2010 6:09 PM (permalink)
                0
                hi all,
                i need help with my logon vbs script.

                then user logons to domain my script finds his username, i know how to do this. let say that username is John. next i need to read some parameters from text file for John i use this part of script

                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objTextFile = objFSO.OpenTextFile _
                ("\\myserver\NETLOGON\DATA\John.txt", ForReading)
                Do Until objTextFile.AtEndOfStream
                strNextLine = objTextFile.Readline
                arrMASIV = Split(strNextLine , ",")
                Loop

                the question is how can i use that script will use not hard coded path to take data from "\\myserver\NETLOGON\DATA\John.txt" ?  i need that for each user it will take data from user own data file, for Leon it must take data from "\\myserver\NETLOGON\DATA\Leon.txt"

                thank you for any help
                sorry for bad english
                <message edited by danygug on Tuesday, November 02, 2010 6:10 PM>
                 
                #8
                  ebgreen

                  • Total Posts : 8227
                  • Scores: 98
                  • Reward points : 0
                  • Joined: 7/12/2005
                  • Status: offline
                  RE: Re: Reading a txt-file with VBScript Wednesday, November 03, 2010 2:05 AM (permalink)
                  0
                  Please create your own thread for your question rather than posting in an old thread.
                  "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                  Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                  http://www.visualbasicscript.com/m_47117/tm.htm
                   
                  #9
                    root_jain

                    • Total Posts : 3
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 1/28/2011
                    • Status: offline
                    RE: Re: Reading a txt-file with VBScript Tuesday, August 30, 2011 12:21 AM (permalink)
                    0
                    Hi ,
                     
                     I want to readline from file and compare it with a string pattern with wildcard. Is there any possible solution for this.
                     
                    #10
                      john7a8

                      • Total Posts : 580
                      • Scores: 35
                      • Reward points : 0
                      • Joined: 1/25/2010
                      • Location: Orlando, FL
                      • Status: offline
                      RE: Re: Reading a txt-file with VBScript Tuesday, August 30, 2011 1:29 AM (permalink)
                      0
                      Did you read the previous post in this thread from ebgreen?
                       
                      In case you missed it "Please create your own thread for your question rather than posting in an old thread."
                       
                      #11

                        Online Bookmarks Sharing: Share/Bookmark

                        Jump to:

                        Current active users

                        There are 0 members and 5 guests.

                        Icon Legend and Permission

                        • New Messages
                        • No New Messages
                        • Hot Topic w/ New Messages
                        • Hot Topic w/o New Messages
                        • Locked w/ New Messages
                        • Locked w/o New Messages
                        • Read Message
                        • Post New Thread
                        • Reply to message
                        • Post New Poll
                        • Submit Vote
                        • Post reward post
                        • Delete my own posts
                        • Delete my own threads
                        • Rate post

                        2000-2012 ASPPlayground.NET Forum Version 3.9