![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | Re: Count lines in another VBS file "XP" <XP@xxxxxx> wrote in message news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx Quote: >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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | Re: Count lines in another VBS file "XP" <XP@xxxxxx> wrote in message news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx Quote: >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. 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 Specs![]() |
| | #5 (permalink) |
| | RE: Count lines in another VBS file The answer is trivial. "Old Pedant" wrote: Quote: > 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 Specs![]() |
| | #6 (permalink) |
| | Re: Count lines in another VBS file On Jul 23, 9:42 pm, "Richard Mueller [MVP]" <rlmueller- nos...@xxxxxx> wrote: Quote: > "XP" <X...@xxxxxx> wrote in message > > news:9A8CC378-D10D-4DE8-A487-C4EB3786CFD6@xxxxxx > Quote: > >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. Quote: > > Can someone help me with that? Quote: > > 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 > -- 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 Specs![]() |
| | #7 (permalink) |
| | Re: Count lines in another VBS file "Tom Lavedas" wrote: Quote: > 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 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 Specs![]() |
| | #8 (permalink) |
| | Re: Count lines in another VBS file Tom Lavedas schrieb: Quote: > On Jul 23, 9:42 pm, "Richard Mueller [MVP]" <rlmueller- > nos...@xxxxxx> wrote: Quote: >> "XP" <X...@xxxxxx> wrote in message Quote: Quote: Quote: >>> I would like to code a VBS file that counts all the lines in another VBS >>> file Quote: Quote: >> lines = Split( inFile.ReadAll, vbLF ) >> lineCount = UBound(lines) ' assumes last line ends with line break Quote: > Here is my candidate for counting lines ... Quote: > 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Add + count pattern occurencies in text file? | VB Script | |||
| Re: best method to count lines | PowerShell | |||
| File Count | Software | |||
| Using lines in a text file | PowerShell | |||
| File count wrong........... | Vista file management | |||