I have what seems to be a very simple issue in a script that I am writing.
The first line in an input file that I'm reading is an arbitrary number, and
every line after that is the meat of the input file. An example of the log
file would look like this:

12345
cf,http://dev.sheeltest.org/sys2/objFolder

How would I read the first line and put it into a variable, and then take
the next line and any subsequent lines in the input line and splits it into
an array?

Any help is greatly appreciated.
Thanks,
Sheel

Re: Reading input file by mayayana

mayayana
Tue Dec 04 08:29:41 PST 2007

Is there any reason they can't be in the same array?
If you just use Textstream.ReadAll to read in the
file, you can then use A1 = Split(filetext, vbCrLf)
to split the file into an array. Then A1(0) is your
variable and you can deal with the other lines
separately by just treating A1 as a 1-base array:

For i = 1 to UBound(A1)


> 12345
> cf,http://dev.sheeltest.org/sys2/objFolder
>
> How would I read the first line and put it into a variable, and then take
> the next line and any subsequent lines in the input line and splits it
into
> an array?
>
> Any help is greatly appreciated.
> Thanks,
> Sheel



Re: Reading input file by ekkehard

ekkehard
Tue Dec 04 08:49:32 PST 2007

Sheel schrieb:
> I have what seems to be a very simple issue in a script that I am writing.
> The first line in an input file that I'm reading is an arbitrary number, and
> every line after that is the meat of the input file. An example of the log
> file would look like this:
>
> 12345
> cf,http://dev.sheeltest.org/sys2/objFolder
>
> How would I read the first line and put it into a variable, and then take
> the next line and any subsequent lines in the input line and splits it into
> an array?
>
> Any help is greatly appreciated.
> Thanks,
> Sheel

Given a sample file "sheel.txt" like this:

12345
cf,http://dev.sheeltest.org/sys2/objFolder
cf,http://dev.sheeltest.org/second/directory
FrsField,SecField

this code:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sIFSpec : sIFSpec = ".\sheel.txt"
Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec )
Dim aLines : aLines = Array()
Dim lNum
Dim sLine, aParts

If oTS.AtEndOfStream Then
WScript.Echo sIFSpec, "is empty"
Else
sLine = oTS.ReadLine
lNum = CLng( sLine ) ' will crash on bad file format
WScript.Echo "magic number at start", lNum
Do Until oTS.AtEndOfStream
sLine = oTS.ReadLine
aParts = Split( sLine, "," )
If 1 = UBound( aParts ) Then
ReDim Preserve aLines( UBound( aLines ) + 1 )
aLines( UBound( aLines ) ) = aParts
' why not do all processing here? do you really need aLines?
Else
' bad file format
End If
Loop
End If
oTS.Close

For Each aParts In aLines
WScript.Echo "|" & Join( aParts, "| " ) & "|"
' do something more interesting with aParts
Next

will output:

=== FileHeader: process file with header ================
magic number at start 12345
|cf| http://dev.sheeltest.org/sys2/objFolder|
|cf| http://dev.sheeltest.org/second/directory|
|FrsField| SecField|
=== FileHeader: 0 done (00:00:00) =======================

If you decide to follow mayayana's suggestion to Split on ReadAll,
be sure to handle the (a) possible empty element(s) at UBound( A1 ) -
caused by (a) trailing vbCrLf(s) in the file.

Re: Reading input file by Tom

Tom
Tue Dec 04 14:38:29 PST 2007

On Dec 4, 11:49 am, "ekkehard.horner" <ekkehard.hor...@arcor.de>
wrote:
> Sheel schrieb:
>
> > I have what seems to be a very simple issue in a script that I am writing.
> > The first line in an input file that I'm reading is an arbitrary number, and
> > every line after that is the meat of the input file. An example of the log
> > file would look like this:
>
> > 12345
> > cf,http://dev.sheeltest.org/sys2/objFolder
>
> > How would I read the first line and put it into a variable, and then take
> > the next line and any subsequent lines in the input line and splits it into
> > an array?
>
> > Any help is greatly appreciated.
> > Thanks,
> > Sheel
>
> Given a sample file "sheel.txt" like this:
>
> 12345
> cf,http://dev.sheeltest.org/sys2/objFolder
> cf,http://dev.sheeltest.org/second/directory
> FrsField,SecField
>
> this code:
>
> Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
> Dim sIFSpec : sIFSpec = ".\sheel.txt"
> Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec )
> Dim aLines : aLines = Array()
> Dim lNum
> Dim sLine, aParts
>
> If oTS.AtEndOfStream Then
> WScript.Echo sIFSpec, "is empty"
> Else
> sLine = oTS.ReadLine
> lNum = CLng( sLine ) ' will crash on bad file format
> WScript.Echo "magic number at start", lNum
> Do Until oTS.AtEndOfStream
> sLine = oTS.ReadLine
> aParts = Split( sLine, "," )
> If 1 = UBound( aParts ) Then
> ReDim Preserve aLines( UBound( aLines ) + 1 )
> aLines( UBound( aLines ) ) = aParts
> ' why not do all processing here? do you really need aLines?
> Else
> ' bad file format
> End If
> Loop
> End If
> oTS.Close
>
> For Each aParts In aLines
> WScript.Echo "|" & Join( aParts, "| " ) & "|"
> ' do something more interesting with aParts
> Next
>
> will output:
>
> === FileHeader: process file with header ================
> magic number at start 12345
> |cf|http://dev.sheeltest.org/sys2/objFolder|
> |cf|http://dev.sheeltest.org/second/directory|
> |FrsField| SecField|
> === FileHeader: 0 done (00:00:00) =======================
>
> If you decide to follow mayayana's suggestion to Split on ReadAll,
> be sure to handle the (a) possible empty element(s) at UBound( A1 ) -
> caused by (a) trailing vbCrLf(s) in the file.

Or maybe ...

Dim oFS : Set oFS =
CreateObject( "Scripting.FileSystemObject" )
Dim sIFSpec : sIFSpec = ".\sheel.txt"
Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec, 1 )
Dim aLines
Dim lNum
Dim sLine, aParts

If oTS.AtEndOfStream Then
WScript.Echo sIFSpec, "is empty"
Else
sLine = oTS.ReadLine
lNum = CLng( sLine ) ' will crash on bad file format
aLines = Split(oTS.Read, vbNewline)
WScript.Echo "magic number at start", lNum
End if
oTS.Close

If the lines need to be parsed at the comma (which the OP did not
request) ...

For i = 0 to UBound(aLines)
aLines(i) = Split(aLines(i), ",")
next

This creates an array of arrays, which are addressed just a bit
differently than a two dimensional array ...

wsh.echo "For example, Line 2=", aLine(1)(0), aLine(1)(1)

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Re: Reading input file by ekkehard

ekkehard
Tue Dec 04 16:14:13 PST 2007

Tom Lavedas schrieb:
> On Dec 4, 11:49 am, "ekkehard.horner" <ekkehard.hor...@arcor.de>
> wrote:
>> Sheel schrieb:
>>
>>> I have what seems to be a very simple issue in a script that I am writing.
>>> The first line in an input file that I'm reading is an arbitrary number, and
>>> every line after that is the meat of the input file. An example of the log
>>> file would look like this:
>>> 12345
>>> cf,http://dev.sheeltest.org/sys2/objFolder
>>> How would I read the first line and put it into a variable, and then take
>>> the next line and any subsequent lines in the input line and splits it into
>>> an array?
>>> Any help is greatly appreciated.
>>> Thanks,
>>> Sheel
>> Given a sample file "sheel.txt" like this:
>>
>> 12345
>> cf,http://dev.sheeltest.org/sys2/objFolder
>> cf,http://dev.sheeltest.org/second/directory
>> FrsField,SecField
>>
>> this code:
>>
>> Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
>> Dim sIFSpec : sIFSpec = ".\sheel.txt"
>> Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec )
>> Dim aLines : aLines = Array()
>> Dim lNum
>> Dim sLine, aParts
>>
>> If oTS.AtEndOfStream Then
>> WScript.Echo sIFSpec, "is empty"
>> Else
>> sLine = oTS.ReadLine
>> lNum = CLng( sLine ) ' will crash on bad file format
>> WScript.Echo "magic number at start", lNum
>> Do Until oTS.AtEndOfStream
>> sLine = oTS.ReadLine
>> aParts = Split( sLine, "," )
>> If 1 = UBound( aParts ) Then
>> ReDim Preserve aLines( UBound( aLines ) + 1 )
>> aLines( UBound( aLines ) ) = aParts
>> ' why not do all processing here? do you really need aLines?
>> Else
>> ' bad file format
>> End If
>> Loop
>> End If
>> oTS.Close
>>
>> For Each aParts In aLines
>> WScript.Echo "|" & Join( aParts, "| " ) & "|"
>> ' do something more interesting with aParts
>> Next
>>
>> will output:
>>
>> === FileHeader: process file with header ================
>> magic number at start 12345
>> |cf|http://dev.sheeltest.org/sys2/objFolder|
>> |cf|http://dev.sheeltest.org/second/directory|
>> |FrsField| SecField|
>> === FileHeader: 0 done (00:00:00) =======================
>>
>> If you decide to follow mayayana's suggestion to Split on ReadAll,
>> be sure to handle the (a) possible empty element(s) at UBound( A1 ) -
>> caused by (a) trailing vbCrLf(s) in the file.
>
> Or maybe ...
>
> Dim oFS : Set oFS =
> CreateObject( "Scripting.FileSystemObject" )
> Dim sIFSpec : sIFSpec = ".\sheel.txt"
> Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec, 1 )
> Dim aLines
> Dim lNum
> Dim sLine, aParts
>
> If oTS.AtEndOfStream Then
> WScript.Echo sIFSpec, "is empty"
> Else
> sLine = oTS.ReadLine
> lNum = CLng( sLine ) ' will crash on bad file format
> aLines = Split(oTS.Read, vbNewline)

Shouldn't that be
aLines = Split(oTS.ReadAll, vbNewline)
Or
aLines = Split(oTS.ReadAll, vbCrLf)

> WScript.Echo "magic number at start", lNum
> End if
> oTS.Close
>
> If the lines need to be parsed at the comma (which the OP did not
> request) ...
>
> For i = 0 to UBound(aLines)
> aLines(i) = Split(aLines(i), ",")
> next
>
> This creates an array of arrays, which are addressed just a bit
> differently than a two dimensional array ...
>
> wsh.echo "For example, Line 2=", aLine(1)(0), aLine(1)(1)
>
> Tom Lavedas
> ===========
> http://members.cox.net/tglbatch/wsh/

Re: Reading input file by Tom

Tom
Tue Dec 04 19:10:29 PST 2007

On Dec 4, 7:14 pm, "ekkehard.horner" <ekkehard.hor...@arcor.de> wrote:
> Tom Lavedas schrieb:
>
>
>
> > On Dec 4, 11:49 am, "ekkehard.horner" <ekkehard.hor...@arcor.de>
> > wrote:
> >> Sheel schrieb:
>
> >>> I have what seems to be a very simple issue in a script that I am writing.
> >>> The first line in an input file that I'm reading is an arbitrary number, and
> >>> every line after that is the meat of the input file. An example of the log
> >>> file would look like this:
> >>> 12345
> >>> cf,http://dev.sheeltest.org/sys2/objFolder
> >>> How would I read the first line and put it into a variable, and then take
> >>> the next line and any subsequent lines in the input line and splits it into
> >>> an array?
> >>> Any help is greatly appreciated.
> >>> Thanks,
> >>> Sheel
> >> Given a sample file "sheel.txt" like this:
>
> >> 12345
> >> cf,http://dev.sheeltest.org/sys2/objFolder
> >> cf,http://dev.sheeltest.org/second/directory
> >> FrsField,SecField
>
> >> this code:
>
> >> Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
> >> Dim sIFSpec : sIFSpec = ".\sheel.txt"
> >> Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec )
> >> Dim aLines : aLines = Array()
> >> Dim lNum
> >> Dim sLine, aParts
>
> >> If oTS.AtEndOfStream Then
> >> WScript.Echo sIFSpec, "is empty"
> >> Else
> >> sLine = oTS.ReadLine
> >> lNum = CLng( sLine ) ' will crash on bad file format
> >> WScript.Echo "magic number at start", lNum
> >> Do Until oTS.AtEndOfStream
> >> sLine = oTS.ReadLine
> >> aParts = Split( sLine, "," )
> >> If 1 = UBound( aParts ) Then
> >> ReDim Preserve aLines( UBound( aLines ) + 1 )
> >> aLines( UBound( aLines ) ) = aParts
> >> ' why not do all processing here? do you really need aLines?
> >> Else
> >> ' bad file format
> >> End If
> >> Loop
> >> End If
> >> oTS.Close
>
> >> For Each aParts In aLines
> >> WScript.Echo "|" & Join( aParts, "| " ) & "|"
> >> ' do something more interesting with aParts
> >> Next
>
> >> will output:
>
> >> === FileHeader: process file with header ================
> >> magic number at start 12345
> >> |cf|http://dev.sheeltest.org/sys2/objFolder|
> >> |cf|http://dev.sheeltest.org/second/directory|
> >> |FrsField| SecField|
> >> === FileHeader: 0 done (00:00:00) =======================
>
> >> If you decide to follow mayayana's suggestion to Split on ReadAll,
> >> be sure to handle the (a) possible empty element(s) at UBound( A1 ) -
> >> caused by (a) trailing vbCrLf(s) in the file.
>
> > Or maybe ...
>
> > Dim oFS : Set oFS =
> > CreateObject( "Scripting.FileSystemObject" )
> > Dim sIFSpec : sIFSpec = ".\sheel.txt"
> > Dim oTS : Set oTS = oFS.OpenTextFile( sIFSpec, 1 )
> > Dim aLines
> > Dim lNum
> > Dim sLine, aParts
>
> > If oTS.AtEndOfStream Then
> > WScript.Echo sIFSpec, "is empty"
> > Else
> > sLine = oTS.ReadLine
> > lNum = CLng( sLine ) ' will crash on bad file format
> > aLines = Split(oTS.Read, vbNewline)
>
> Shouldn't that be
> aLines = Split(oTS.ReadAll, vbNewline)
> Or
> aLines = Split(oTS.ReadAll, vbCrLf)
>
> > WScript.Echo "magic number at start", lNum
> > End if
> > oTS.Close
>
> > If the lines need to be parsed at the comma (which the OP did not
> > request) ...
>
> > For i = 0 to UBound(aLines)
> > aLines(i) = Split(aLines(i), ",")
> > next
>
> > This creates an array of arrays, which are addressed just a bit
> > differently than a two dimensional array ...
>
> > wsh.echo "For example, Line 2=", aLine(1)(0), aLine(1)(1)
>
> > Tom Lavedas
> > ===========
> >http://members.cox.net/tglbatch/wsh/

Yes, of course. It was a C & P error - forgot to go back and finish
the thought.

Thanks.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/