1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VBScript - How to output array items into a text file?

Discussion in 'HTML, Graphics & Programming' started by DJMK4, Mar 24, 2010.

  1. DJMK4


    Joined: Dec 1, 2004

    Posts: 21,725

    Location: S.Wales, Cardiff


    What im trying to achieve is to write some code to intergrate with othercode specifically to get it to output what ever is being stored in the array to a text file.

    This is what I have at the moment:

    Option Explicit
    Dim objFSO, objFolder, objShell, objTextFile, objFile
    Dim strDirectory, strFile, strText
    strDirectory = "e:\logs3"
    strFile = "\Summer.txt"
    strText = "Book Another Holiday"
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
       Set objFolder = objFSO.CreateFolder(strDirectory)
       WScript.Echo "Just created " & strDirectory
    End If
    If objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
       Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
       Wscript.Echo "Just created " & strDirectory & strFile
    End If
    set objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 8
    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)
    ' Writes strText every time you run this VBScript
    ' Bonus or cosmetic section to launch explorer to check file
    If err.number = vbEmpty then
       Set objShell = CreateObject("WScript.Shell")
       objShell.run ("Explorer" &" " & strDirectory & "\" )
    Else WScript.echo "VBScript Error: " & err.number
    End If
    ' End of VBScript to write to a file with error-correcting Code
    Although im not sure 100% what I should be puttin in this line as this is the code that will write to the text file.

    ' Writes strText every time you run this VBScript
    What I need to do is change this from string to array?

    If anyone could give me some tips that would be great

    Thanks :)
  2. Spunkey


    Joined: Oct 18, 2002

    Posts: 12,785

    Location: The land of milk & beans

    To loop through your array would be...

      for i = o to ubound(array)
        strText = strText & array(i) & vbCrLf      ' the vbCrLf will add a line break after each item

Share This Page