Overclockers UK Forums Click here for more details
Free Shipping for Loyal Forum Members - CLICK HERE

Go Back   Overclockers UK Forums > Software > HTML, Graphics & Programming

Reply
 
Thread Tools
Old 24th Mar 2010, 09:15   #1
DJMK4
Capodecina
 
DJMK4's Avatar
 
Joined: Dec 2004
Location: S.Wales, Cardiff
Posts: 21,449
VBScript - How to output array items into a text file?

Hi,

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:

Code:
 



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)
Else
   Set objFolder = objFSO.CreateFolder(strDirectory)
   WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
   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
objTextFile.WriteLine(strText)
objTextFile.Close

' 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

WScript.Quit

' 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.

Code:
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
What I need to do is change this from string to array?

If anyone could give me some tips that would be great

Thanks

DJMK4 is offline   View trust for Reply With Quote
Old 24th Mar 2010, 09:48   #2
Spunkey
Capodecina
 
Spunkey's Avatar
 
Joined: Oct 2002
Location: The land of milk & beans
Posts: 12,479
To loop through your array would be...

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

  objTextFile.Close

Spunkey is offline   View trust for Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 23:27.


Powered by vBulletin®
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.
Overclockers UK (Ocuk Ltd)