Windows Vista Forums

Count lines in another VBS file
  1. #1


    XP Guest

    Count lines in another VBS file

    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.



      My System SpecsSystem Spec

  2. #2


    Pegasus \(MVP\) Guest

    Re: Count lines in another VBS file


    "XP" <XP@xxxxxx> wrote in message
    news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx

    >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.
    Let's have a look at the code you've got so far.



      My System SpecsSystem Spec

  3. #3


    Old Pedant Guest

    RE: Count lines in another VBS file

    Counting the lines is trivial:

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set inFile = FSO.OpenTextFile( "c:\full\path\to\somefile.vbs" )
    lines = Split( inFile.ReadAll, vbLF )
    lineCount = UBound(lines) ' assumes last line ends with line break

    But I haven't the foggiest idea what "adding a CONST to the full file name
    of the target" is supposed to mean.

    I don't even know what a "target" is in this case. And I don't know why you
    would want to add "CONST" to a file name.



      My System SpecsSystem Spec

  4. #4


    Richard Mueller [MVP] Guest

    Re: Count lines in another VBS file


    "XP" <XP@xxxxxx> wrote in message
    news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx

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



      My System SpecsSystem Spec

  5. #5


    XP Guest

    RE: Count lines in another VBS file


    The answer is trivial.

    "Old Pedant" wrote:

    > Counting the lines is trivial:
    >
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > Set inFile = FSO.OpenTextFile( "c:\full\path\to\somefile.vbs" )
    > lines = Split( inFile.ReadAll, vbLF )
    > lineCount = UBound(lines) ' assumes last line ends with line break
    >
    > But I haven't the foggiest idea what "adding a CONST to the full file name
    > of the target" is supposed to mean.
    >
    > I don't even know what a "target" is in this case. And I don't know why you
    > would want to add "CONST" to a file name.
    >
    >

      My System SpecsSystem Spec

  6. #6


    Tom Lavedas Guest

    Re: Count lines in another VBS file

    On Jul 23, 9:42 pm, "Richard Mueller [MVP]" <rlmueller-
    nos...@xxxxxx> wrote:

    > "XP" <X...@xxxxxx> wrote in message
    >
    > news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx
    >

    > >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/

      My System SpecsSystem Spec

  7. #7


    Old Pedant Guest

    Re: Count lines in another VBS file

    "Tom Lavedas" wrote:

    > Here is my candidate for counting lines ...
    >
    > Const ForAppending = 8
    > Dim sInFile, nLines
    >
    > 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
    CUTE TRICK!

    Mind you, it's a fib that the approach "does not need to read the file at
    all." Because text files are just stored as strings of characters in Windows
    operating systems (and in Linux/Unix; you have to get to IBM mainframe before
    the file system itself is aware of "lines", and even then it's optional), the
    only way for the FSO COM code to figure out how many lines there are is to go
    out and read the file, under the covers.

    BUT...

    But it's clearly true that such an operation is going to be enormously more
    efficient than is pulling the text into a VBScript string! If nothing else,
    unless the text file is a Unicode file, a translation of some sort has to
    take place on a character by character basis, because internally VBS only
    uses Unicode.

    Probably faster than "ReadAll" by over an order of magnitude, and that's
    even before we do the silly "Split" that I proposed.

    Nice one to remember.



      My System SpecsSystem Spec

  8. #8


    ekkehard.horner Guest

    Re: Count lines in another VBS file

    Tom Lavedas schrieb:

    > On Jul 23, 9:42 pm, "Richard Mueller [MVP]" <rlmueller-
    > nos...@xxxxxx> wrote:

    >> "XP" <X...@xxxxxx> wrote in message
    [...]

    >>> I would like to code a VBS file that counts all the lines in another VBS
    >>> file
    [...]

    >> lines = Split( inFile.ReadAll, vbLF )
    >> lineCount = UBound(lines) ' assumes last line ends with line break
    [...]

    > Here is my candidate for counting lines ...
    [...]

    > nLines = .OpenTextFile(sInFile, ForAppending).Line
    [...]

    Tom's approach really is ingenious. But you have to be aware
    of some borderline cases, where .Line yields surprising results:
    empty files (1) and files consisting of line endings only (2, 3):

    === countLines: count lines in file ===================================
    ==== DOS A empty 0 0 0 0 ><
    1 B ALL L Use ForAppending 1 0 Falsch
    ==== DOS B one sep 0 1 0 2 >.<
    2 B ALL L Use ForAppending 2 1 Falsch
    ==== DOS C three seps 0 3 0 6 >...<
    3 B ALL L Use ForAppending 4 3 Falsch
    ==== DOS D 1 elm 1 1 1 1 >1<
    4 B ALL L Use ForAppending 1 1 Wahr
    ==== DOS E 1 elm, tail 1 2 1 3 >1.<
    5 B ALL L Use ForAppending 2 2 Wahr
    ==== DOS F no blanks 3 3 3 7 >1.2.3<
    6 B ALL L Use ForAppending 3 3 Wahr
    ==== DOS G no blanks, tail 3 4 3 9 >1.2.3.<
    7 B ALL L Use ForAppending 4 4 Wahr
    ==== DOS H blanks 4 5 5 12 >1..2.3.4<
    8 B ALL L Use ForAppending 5 5 Wahr
    ==== DOS I blanks, tail 4 6 5 14 >1..2.3.4.<
    9 B ALL L Use ForAppending 6 6 Wahr
    ==== DOS J no blanks, tails 5 7 5 17 >1.2.3.4.5..<
    10 B ALL L Use ForAppending 7 7 Wahr
    ==== DOS K blanks, tails 6 9 7 22 >1..2.3.4.5.6..<
    11 B ALL L Use ForAppending 9 9 Wahr
    === countLines: 0 done (00:00:00) =====================================

    The "Append/Line" method defines "lines in a file" as "what line
    would be written to if text would be appended". This may be the
    information you are interested in - or not.

    Other meanings of "lines in file" are reasonable - if you have to
    pay a programmer by line, you may prefer "non empty lines". That
    would give 6 for (11).

    If your task is to append to many different files in an economic
    way, "all lines except trailing blanks" may be appropriate.

    If you have to inspect the lines, you must read (.ReadLine or .ReadAll
    or .Read( SizeOfFile)) the file. Using Split() just to count the
    the vbLf (or, the separator used in the file (dos, unix, mac)) does
    not seem silly to me, although I think a RegExp and its Matches.Count
    are better (not based on evidence!). But Split as preparation for
    an inspection of the lines in the delivered array looks reasonable.

    So I think further study is needed.



      My System SpecsSystem Spec

Count lines in another VBS file problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get-ChildItem.Count not working with one file steve PowerShell 2 14 Jan 2010
Wrong file count when copying Electro General Discussion 3 24 Nov 2009
Re: best method to count lines Kiron PowerShell 1 02 Feb 2009
File Count ilyabyk83 Software 4 07 Jan 2009
File count wrong........... =?Utf-8?B?V2Fyd2ljayBXZWJi?= Vista file management 0 29 Sep 2006