Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load values from a text file into a Array

Status
Not open for further replies.

flybravo

Programmer
Jun 30, 2003
35
US
Here is my script so far. When I try to reyurn the values in the array, I only get the last row in the textfile


This is what the file looks like;

AIX36009
AIX36009P01
AIX36009D01

I need array(0) to equal AIX36009 so on. Some text files might have more lines then some other ones


Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objOpen = objFSO.OpenTextFile("C:\ActiveJobs\AIX\AIX36009\AIX36009_Req.txt", ForReading)
dim arrFileName()

i = 0
Do While objOpen.AtEndOfLine <> true
i = i + 1
text = objOpen.ReadLine
'WSCript.Echo text
ReDim arrFileName(i)
arrFilename(i - 1) = text


loop

WSCript.Echo arrFilename(0)
 
Hello flybravo,

Try this.
Code:
'-----
Const ForReading = 1
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objOpen = objFSO.OpenTextFile(&quot;C:\ActiveJobs\AIX\AIX36009\AIX36009_Req.txt&quot;, ForReading)
Dim arrFileName()

i = 0
Do While objOpen.AtEndOfStream <> true
	text = objOpen.ReadLine
	'WSCript.Echo text
        ReDim Preserve arrFileName(i)
	arrFileName(i) = text
	i=i+1
loop
objOpen.Close
Set objOpen = Nothing
Set objFSO = Nothing

'WSCript.Echo arrFilename(0)

For i=0 to UBound(arrFileName)
	WScript.Echo arrFileName(i)
Next
'-----
regards - tsuji
 
Thanks for your help. The script works perfect
 
This might bre aliitle quicker
Const ForReading = 1
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objOpen = objFSO.OpenTextFile(&quot;C:\ActiveJobs\AIX\AIX36009\AIX36009_Req.txt&quot;, ForReading)
Dim arrFileName()

FileContent = objopen.ReadAll
arrFileName = Split(FileContent, VbCrLF)

objOpen.Close
Set objOpen = Nothing
Set objFSO = Nothing

For i=0 to UBound(arrFileName)
WScript.Echo arrFileName(i)
Next
 
This might be a liitle quicker

Const ForReading = 1
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objOpen = objFSO.OpenTextFile(&quot;C:\ActiveJobs\AIX\AIX36009\AIX36009_Req.txt&quot;, ForReading)
Dim arrFileName()

FileContent = objopen.ReadAll
arrFileName = Split(FileContent, VbCrLF)

objOpen.Close
Set objOpen = Nothing
Set objFSO = Nothing

For i=0 to UBound(arrFileName)
WScript.Echo arrFileName(i)
Next
 
quicker to type out the code of quicker to actually run?? ;-)
 
Quicker to run, as you haven't to redim preserve the array.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top