I am trying to accomplish the same thing as what the FOR /F loop does for
CSV files under NT shell, only under VBS. I would like to be able to specify
tokens, delimiters, skip lines, eol, etc. as easily as it can be done in an
NT shell FOR /F loop, but I have yet to find anything on this!!!! This has
been such a useful function of NT shell, that I can't believe there isn't
something like this built into WSH already! I have included part of the FOR
/? output to hopefully clarify what I am trying to do...
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
filenameset is one or more file names. Each file is opened, read
and processed before going on to the next file in filenameset.
Processing consists of reading in the file, breaking it up into
individual lines of text and then parsing each line into zero or
more tokens. The body of the for loop is then called with the
variable value(s) set to the found token string(s). By default, /F
passes the first blank separated token from each line of each file.
Blank lines are skipped. You can override the default parsing
behavior by specifying the optional "options" parameter. This
is a quoted string which contains one or more keywords to specify
different parsing options. The keywords are:
eol=c - specifies an end of line comment character
(just one)
skip=n - specifies the number of lines to skip at the
beginning of the file.
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens from each line are to
be passed to the for body for each iteration.
This will cause additional variable names to
be allocated. The m-n form is a range,
specifying the mth through the nth tokens. If
the last character in the tokens= string is an
asterisk, then an additional variable is
allocated and receives the remaining text on
the line after the last token parsed.
Some examples might help:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
would parse each line in myfile.txt, ignoring lines that begin with
a semicolon, passing the 2nd and 3rd token from each line to the for
body, with tokens delimited by commas and/or spaces. Notice the for
body statements reference %i to get the 2nd token, %j to get the
3rd token, and %k to get all remaining tokens after the 3rd. For
file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.
%i is explicitly declared in the for statement and the %j and %k
are implicitly declared via the tokens= option. You can specify up
to 26 tokens via the tokens= line, provided it does not cause an
attempt to declare a variable higher than the letter 'z' or 'Z'.
Remember, FOR variable names are case sensitive, global, and you
can't have more than 52 total active at any one time.
You can also use the FOR /F parsing logic on an immediate string, by
making the filenameset between the parenthesis a quoted string,
using single quote characters. It will be treated as a single line
of input from a file and parsed.
Here's an example that takes a block of line-broken data(sData) delimited with
commas, for example, and splits everything into separate lines, then lets you
run a command of some kind on the trimmed text:
aData = Split(sData, vbCrLf)
For i = 0 to Ubound(aData)
aTmp = Split(aData(i), ",")
For j = 0 to Ubound(aTmp)
WScript.Echo aTmp(j)
Next
Next
--
Please respond in the newsgroup so everyone may benefit.
http://dev.remotenetworktechnology.com
----------
Subscribe to Microsoft's Security Bulletins:
http://www.microsoft.com/technet/security/bulletin/notify.asp
"tbader01" <tracey...@eds.com> wrote in message
news:uAHySUQmCHA.2340@tkmsftngp07...
How could I specify say, the first and third tokens only?
I have rewritten and added to it to make more sense to me, which hopefully
makes still makes sense to you:
Dim fso, MyFile, ReadLineTextFile, ReadAllTextFile
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("D:\SLM\AAPAGENT.csv", ForReading)
ReadLineTextFile = MyFile.ReadLine
ReadAllTextFile = MyFile.ReadAll
EachLine = Split(ReadAllTextFile, vbCrLf)
Dim l
For l = 0 to Ubound(EachLine)
'The line Below will display each line of MyFile...
'WScript.Echo EachLine(l)
' Display each Token
EachToken = Split(EachLine(l), ",")
WScript.Echo "There are: " & ubound(EachToken) & " Tokens total."
Dim t
For t = 0 to Ubound(EachToken)
WScript.Echo EachToken(t)
Next
WScript.Echo
Next
WScript.Echo "There are: " & ubound(EachLine) & " Lines total."
MyFile.Close
"Alex K. Angelopoulos (MVP)" <a...@mvps.org> wrote in message
news:#omuSBVmCHA.2708@tkmsftngp04...
' Display each Token
EachToken = Split(EachLine(l), ",")
WScript.Echo "There are: " & ubound(EachToken) & " Tokens total."
WScript.Echo "The first token is", EachToken(0)
WScript.Echo "The third token is", EachToken(2)
--
Please respond in the newsgroup so everyone may benefit.
http://dev.remotenetworktechnology.com
----------
Subscribe to Microsoft's Security Bulletins:
http://www.microsoft.com/technet/security/bulletin/notify.asp
"tbader01" <tracey...@eds.com> wrote in message
news:O7zU8qXmCHA.2360@tkmsftngp07...