Tom
Thu Jul 24 07:48:57 CDT 2008
On Jul 23, 9:42 pm, "Richard Mueller [MVP]" <rlmueller-
nos...@ameritech.nospam.net> wrote:
> "XP" <X...@discussions.microsoft.com> wrote in message
>
> news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@microsoft.com...
>
> >I would like to code a VBS file that counts all the lines in another VBS
> >file
> > by adding a CONST to the full file name of the target.
>
> > Can someone help me with that?
>
> > Thanks much in advance.
>
> Maybe you want to pass the filename and path of the "target" *.vbs files as
> an argument to the VBScript program. If so, the code posted previously could
> be revised as follows to accept an argument:
> ==========
> ' Make sure one argument passed to the program.
> If (Wscript.Arguments.Count <> 1 0) Then
> Wscript.Echo "Require argument missing"
> Wscript.Quit
> End If
>
> ' Read the first argument (index 0).
> strFile = Wscript.Arguments(0).
>
> Set FSO = CreateObject("Scripting.FileSystemObject")
> Set inFile = FSO.OpenTextFile(strFile)
> lines = Split( inFile.ReadAll, vbLF )
> lineCount = UBound(lines) ' assumes last line ends with line break
> Wscript.Echo "Number of lines: " & lineCount
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab -
http://www.rlmueller.net
> --
Here is my candidate for counting lines ...
Const ForAppending = 8
Dim sInFile, nLines
if wsh.arguments.count > 0 Then
sInFile = wsh.arguments(0)
with CreateObject("Scripting.FileSystemObject")
if .FileExists(sInFile) then
'
' Get the number of lines in file
'
nLines = .OpenTextFile(sInFile, ForAppending).Line
wsh.echo sInFile, " lines:", nLines
else
wsh.echo "No file by that name found"
end if
end with ' fso
else
wsh.echo "No file name"
end if
Note that this approach does not need to read the file at all.
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/