![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Nifty line counter needed Hi, I hope someone can help as I need a vbscript in a hurry. I have a number of text files in a specific folder. I need to count the number of lines in each text file in that folder and create an output file with the name of the text file and the number of lines. Then sort the output file in alphabetical order of the text file names. I know how to count lines in any specific file but I'm not so good with the "for each file in this folder" business. Here's what I did with for counting lines. Most of it is probably copied from somewhere. -------- 'Create a File System Object Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Set MyFileContents = Nothing Dim InputFile InputFile = InputBox("Nom du fichier") 'Get the file contents Dim MyFileContents Set MyFileContents = fso.OpenTextFile(InputFile) 'Loop through counting the lines ' Do While Not MyFileContents.AtEndOfStream s= MyFileContents.ReadLIne linelength = len(S) if linelength < 10 then msgbox ctr end if 'msgbox "x" & x & vbcrlf &_ ' "linelength " & linelength & vbcrlf if x <= linelength then x = linelength end if ctr = ctr + 1 Loop 'Return the file's line count msgbox "File name " & InputFile & vbcrlf &_ "FileLineCount " & Ctr & vbcrlf &_ "Maximum Line length " & x 'Cleanup MyFileContents.Close Set MyFileContents = Nothing Set fso = Nothing -------- TIA prebble |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Nifty line counter needed There's nothing wrong with the ReadLine method. (It's not clear what all that "x" code is about.) Personally it seems easier to me (and maybe a bit quicker) to just do: Set FileContents = fso.OpenTextFile(InputFile, 1) s = FileContents.ReadAll FileContents.close Set FileContents = Nothing A = Split(s, vbCrLf) LineCount = UBound(A) + 1 '-- You'll need error trapping for blank files and may want to make other minor adjustments for things like multiple vbCrLf that may appear at end of files. For multiple files you'll probably want to put your counter code into a function. Then do something like: ----------------------------------------- Set oFol = FSO.GetFolder("folderpath") Set oFils = oFol.Files For Each oFil in oFils LineCount = GetLineCount(oFil.Path) '-- maintain a list of files and lengths: sList = sList & oFil.Path & "Linecount: " & LineCount & vbCrLf Next Set oFils = Nothing Set oFol = Nothing Function GetLineCount(FilePath) Dim TS, s, A On Error Resume Next Set TS = fso.OpenTextFile(FilePath, 1) s = TS.ReadAll TS.close Set TS = Nothing A = Split(s, vbCrLf) GetLineCount = UBound(A) + 1 End Function '-- At this point you have a string/list of file paths and their line counts that you can just write to disk. Quote: > I hope someone can help as I need a vbscript in a hurry. > > > I have a number of text files in a specific folder. > I need to count the number of lines > in each text file in that folder and > create an output file with the name of the text file > and the number of lines. > > Then sort the output file in alphabetical order of the text file names. > > I know how to count lines in any specific file but > I'm not so good with the "for each file in this folder" business. > > > Here's what I did with for counting lines. > Most of it is probably copied from somewhere. > -------- > 'Create a File System Object > Dim fso > Set fso = CreateObject("Scripting.FileSystemObject") > Set MyFileContents = Nothing > > Dim InputFile > InputFile = InputBox("Nom du fichier") > > 'Get the file contents > Dim MyFileContents > Set MyFileContents = fso.OpenTextFile(InputFile) > > 'Loop through counting the lines > ' > Do While Not MyFileContents.AtEndOfStream > s= MyFileContents.ReadLIne > > linelength = len(S) > > if linelength < 10 then > msgbox ctr > end if > 'msgbox "x" & x & vbcrlf &_ > ' "linelength " & linelength & vbcrlf > if x <= linelength then > x = linelength > end if > ctr = ctr + 1 > Loop > > 'Return the file's line count > > > msgbox "File name " & InputFile & vbcrlf &_ > "FileLineCount " & Ctr & vbcrlf &_ > "Maximum Line length " & x > > 'Cleanup > MyFileContents.Close > Set MyFileContents = Nothing > Set fso = Nothing > -------- > TIA > prebble > > > > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Nifty line counter needed On Aug 5, 10:23 am, "mayayana" <mayaXXy...@xxxxxx> wrote: Quote: > There's nothing wrong with the ReadLine method. > (It's not clear what all that "x" code is about.) > Personally it seems easier to me (and maybe > a bit quicker) to just do: > > Set FileContents = fso.OpenTextFile(InputFile, 1) > s = FileContents.ReadAll > FileContents.close > Set FileContents = Nothing > > A = Split(s, vbCrLf) > LineCount = UBound(A) + 1 > > '-- You'll need error trapping for blank files and may > want to make other minor adjustments for things like > multiple vbCrLf that may appear at end of files. > > For multiple files you'll probably want to put > your counter code into a function. Then do something > like: > > ----------------------------------------- > > Set oFol = FSO.GetFolder("folderpath") > Set oFils = oFol.Files > For Each oFil in oFils > LineCount = GetLineCount(oFil.Path) > '-- maintain a list of files and lengths: > sList = sList & oFil.Path & "Linecount: " & LineCount & vbCrLf > Next > Set oFils = Nothing > Set oFol = Nothing > > Function GetLineCount(FilePath) > Dim TS, s, A > On Error Resume Next > Set TS = fso.OpenTextFile(FilePath, 1) > s = TS.ReadAll > TS.close > Set TS = Nothing > > A = Split(s, vbCrLf) > GetLineCount = UBound(A) + 1 > End Function > > '-- At this point you have a string/list of file paths > and their line counts that you can just write to disk. > Quote: > > I hope someone can help as I need a vbscript in a hurry. Quote: > > I have a number of text files in a specific folder. > > I need to count the number of lines > > in each text file in that folder and > > create an output file with the name of the text file > > and the number of lines. Quote: > > Then sort the output file in alphabetical order of the text file names. Quote: > > I know how to count lines in any specific file but > > I'm not so good with the "for each file in this folder" business. Quote: > > Here's what I did with for counting lines. > > Most of it is probably copied from somewhere. > > -------- > > 'Create a File System Object > > Dim fso > > Set fso = CreateObject("Scripting.FileSystemObject") > > Set MyFileContents = Nothing Quote: > > Dim InputFile > > InputFile = InputBox("Nom du fichier") Quote: > > 'Get the file contents > > Dim MyFileContents > > Set MyFileContents = fso.OpenTextFile(InputFile) Quote: > > 'Loop through counting the lines > > ' > > Do While Not MyFileContents.AtEndOfStream > > s= MyFileContents.ReadLIne Quote: > > linelength = len(S) Quote: > > if linelength < 10 then > > msgbox ctr > > end if > > 'msgbox "x" & x & vbcrlf &_ > > ' "linelength " & linelength & vbcrlf > > if x <= linelength then > > x = linelength > > end if > > ctr = ctr + 1 > > Loop Quote: > > 'Return the file's line count Quote: > > msgbox "File name " & InputFile & vbcrlf &_ > > "FileLineCount " & Ctr & vbcrlf &_ > > "Maximum Line length " & x Quote: > > 'Cleanup > > MyFileContents.Close > > Set MyFileContents = Nothing > > Set fso = Nothing > > -------- > > TIA > > prebble Function LineCount(sFilespec) Const ForAppending = 8 with CreateObject("Scripting.FileSystemObject") LineCount = .OpenTextFile(sFilespec, ForAppending).Line end with end Function I posted an example based on this technique a few days ago. And as I recall, someone else showed it to me a few years back. I am told that it is off by one count for empty files and files full of blank lines (new line characters only - no other text). Tom Lavedas =========== http://members.cox.net/tglbatch/wsh/ |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Nifty line counter needed "Tom Lavedas" <tglbatch@xxxxxx> wrote in message news:360fd3ac-76f8-45cc-ac08-4670445e4cf0@xxxxxx Quote: > On Aug 5, 10:23 am, "mayayana" <mayaXXy...@xxxxxx> wrote: Quote: >> There's nothing wrong with the ReadLine method. >> (It's not clear what all that "x" code is about.) >> Personally it seems easier to me (and maybe >> a bit quicker) to just do: >> >> Set FileContents = fso.OpenTextFile(InputFile, 1) >> s = FileContents.ReadAll >> FileContents.close >> Set FileContents = Nothing >> >> A = Split(s, vbCrLf) >> LineCount = UBound(A) + 1 >> >> '-- You'll need error trapping for blank files and may >> want to make other minor adjustments for things like >> multiple vbCrLf that may appear at end of files. >> >> For multiple files you'll probably want to put >> your counter code into a function. Then do something >> like: >> >> ----------------------------------------- >> >> Set oFol = FSO.GetFolder("folderpath") >> Set oFils = oFol.Files >> For Each oFil in oFils >> LineCount = GetLineCount(oFil.Path) >> '-- maintain a list of files and lengths: >> sList = sList & oFil.Path & "Linecount: " & LineCount & vbCrLf >> Next >> Set oFils = Nothing >> Set oFol = Nothing >> >> Function GetLineCount(FilePath) >> Dim TS, s, A >> On Error Resume Next >> Set TS = fso.OpenTextFile(FilePath, 1) >> s = TS.ReadAll >> TS.close >> Set TS = Nothing >> >> A = Split(s, vbCrLf) >> GetLineCount = UBound(A) + 1 >> End Function >> >> '-- At this point you have a string/list of file paths >> and their line counts that you can just write to disk. >> Quote: >> > I hope someone can help as I need a vbscript in a hurry. Quote: >> > I have a number of text files in a specific folder. >> > I need to count the number of lines >> > in each text file in that folder and >> > create an output file with the name of the text file >> > and the number of lines. Quote: >> > Then sort the output file in alphabetical order of the text file names. Quote: >> > I know how to count lines in any specific file but >> > I'm not so good with the "for each file in this folder" business. Quote: >> > Here's what I did with for counting lines. >> > Most of it is probably copied from somewhere. >> > -------- >> > 'Create a File System Object >> > Dim fso >> > Set fso = CreateObject("Scripting.FileSystemObject") >> > Set MyFileContents = Nothing Quote: >> > Dim InputFile >> > InputFile = InputBox("Nom du fichier") Quote: >> > 'Get the file contents >> > Dim MyFileContents >> > Set MyFileContents = fso.OpenTextFile(InputFile) Quote: >> > 'Loop through counting the lines >> > ' >> > Do While Not MyFileContents.AtEndOfStream >> > s= MyFileContents.ReadLIne Quote: >> > linelength = len(S) Quote: >> > if linelength < 10 then >> > msgbox ctr >> > end if >> > 'msgbox "x" & x & vbcrlf &_ >> > ' "linelength " & linelength & vbcrlf >> > if x <= linelength then >> > x = linelength >> > end if >> > ctr = ctr + 1 >> > Loop Quote: >> > 'Return the file's line count Quote: >> > msgbox "File name " & InputFile & vbcrlf &_ >> > "FileLineCount " & Ctr & vbcrlf &_ >> > "Maximum Line length " & x Quote: >> > 'Cleanup >> > MyFileContents.Close >> > Set MyFileContents = Nothing >> > Set fso = Nothing >> > -------- >> > TIA >> > prebble > Here is my contestant for the quickest/simplest file line counter ... > > Function LineCount(sFilespec) > Const ForAppending = 8 > with CreateObject("Scripting.FileSystemObject") > LineCount = .OpenTextFile(sFilespec, ForAppending).Line > end with > end Function > > I posted an example based on this technique a few days ago. And as I > recall, someone else showed it to me a few years back. I am told that > it is off by one count for empty files and files full of blank lines > (new line characters only - no other text). counting purposes) before we can count them. If a file can contain more than one empty line, then an empty file must contain one empty line. And perhaps the definition of a line separator is necessary, such as vbLf. With these definitions, the following script shows that your function produces correct results; the script is attached also to avoid line wrap problems: Option Explicit MsgBox "started in " & vbCrLf & _ WScript.CreateObject("WScript.Shell").CurrentDirectory fOverWrite "dummy.txt", "" MsgBox "Line count for empty file -- should be one (for one empty line): " & LineCount("dummy.txt") fOverWrite "dummy.txt", vbCrLf MsgBox "Line count for CrLf file -- should be two (for two empty lines): " & LineCount("dummy.txt") fOverWrite "dummy.txt", "Now is the time for all ..." MsgBox "Line count for one-line file -- should be one: " & LineCount("dummy.txt") fOverWrite "dummy.txt", "Now is the" & vbCrLf & " time for all ..." MsgBox "Line count for two-line file -- should be two: " & LineCount("dummy.txt") fOverWrite "dummy.txt", "Now is the" & vbCrLf & vbCr & " time for all ..." MsgBox "Line count for two-line file -- should be two: " & LineCount("dummy.txt") fOverWrite "dummy.txt", "Now is the" & vbCrLf & vbLf & " time for all ..." MsgBox "Line count for three-line file -- should be three: " & LineCount("dummy.txt") Function LineCount(sFilespec) Const ForAppending = 8 with CreateObject("Scripting.FileSystemObject") LineCount = .OpenTextFile(sFilespec, ForAppending).Line end with end Function Sub fOverWrite(FilePath, sData) 'Try writing ASCII file. 'If error occurs, try writing Unicode file Dim objFSO 'Global file system object Set objFSO = CreateObject("Scripting.FileSystemObject") Dim oTS Set oTS = objFSO.OpenTextFile(FilePath, 2, true) On Error Resume Next oTS.Write sData If Err then On Error GoTo 0 oTS.close Set oTS = objFSO.OpenTextFile(FilePath, 2, true, -1) On Error Resume Next oTS.Write sData If Err Then msgbox "Error in fOverWrite - tried ANSI and Unicode" & vbcrlf & _ "FilePath = " & FilePath & vbcrlf & _ "sData = " & sData wscript.quit End If End If On Error GoTo 0 oTS.Close End Sub -Paul Randall |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Nifty line counter needed try find /c /v "" filenamehere.txt ouput will be: ---------- filenamehere.txt: 138 -- soler "Paul Randall" wrote: Quote: > > "Tom Lavedas" <tglbatch@xxxxxx> wrote in message > news:360fd3ac-76f8-45cc-ac08-4670445e4cf0@xxxxxx Quote: > > On Aug 5, 10:23 am, "mayayana" <mayaXXy...@xxxxxx> wrote: Quote: > >> There's nothing wrong with the ReadLine method. > >> (It's not clear what all that "x" code is about.) > >> Personally it seems easier to me (and maybe > >> a bit quicker) to just do: > >> > >> Set FileContents = fso.OpenTextFile(InputFile, 1) > >> s = FileContents.ReadAll > >> FileContents.close > >> Set FileContents = Nothing > >> > >> A = Split(s, vbCrLf) > >> LineCount = UBound(A) + 1 > >> > >> '-- You'll need error trapping for blank files and may > >> want to make other minor adjustments for things like > >> multiple vbCrLf that may appear at end of files. > >> > >> For multiple files you'll probably want to put > >> your counter code into a function. Then do something > >> like: > >> > >> ----------------------------------------- > >> > >> Set oFol = FSO.GetFolder("folderpath") > >> Set oFils = oFol.Files > >> For Each oFil in oFils > >> LineCount = GetLineCount(oFil.Path) > >> '-- maintain a list of files and lengths: > >> sList = sList & oFil.Path & "Linecount: " & LineCount & vbCrLf > >> Next > >> Set oFils = Nothing > >> Set oFol = Nothing > >> > >> Function GetLineCount(FilePath) > >> Dim TS, s, A > >> On Error Resume Next > >> Set TS = fso.OpenTextFile(FilePath, 1) > >> s = TS.ReadAll > >> TS.close > >> Set TS = Nothing > >> > >> A = Split(s, vbCrLf) > >> GetLineCount = UBound(A) + 1 > >> End Function > >> > >> '-- At this point you have a string/list of file paths > >> and their line counts that you can just write to disk. > >> > >> > I hope someone can help as I need a vbscript in a hurry. > >> > >> > I have a number of text files in a specific folder. > >> > I need to count the number of lines > >> > in each text file in that folder and > >> > create an output file with the name of the text file > >> > and the number of lines. > >> > >> > Then sort the output file in alphabetical order of the text file names. > >> > >> > I know how to count lines in any specific file but > >> > I'm not so good with the "for each file in this folder" business. > >> > >> > Here's what I did with for counting lines. > >> > Most of it is probably copied from somewhere. > >> > -------- > >> > 'Create a File System Object > >> > Dim fso > >> > Set fso = CreateObject("Scripting.FileSystemObject") > >> > Set MyFileContents = Nothing > >> > >> > Dim InputFile > >> > InputFile = InputBox("Nom du fichier") > >> > >> > 'Get the file contents > >> > Dim MyFileContents > >> > Set MyFileContents = fso.OpenTextFile(InputFile) > >> > >> > 'Loop through counting the lines > >> > ' > >> > Do While Not MyFileContents.AtEndOfStream > >> > s= MyFileContents.ReadLIne > >> > >> > linelength = len(S) > >> > >> > if linelength < 10 then > >> > msgbox ctr > >> > end if > >> > 'msgbox "x" & x & vbcrlf &_ > >> > ' "linelength " & linelength & vbcrlf > >> > if x <= linelength then > >> > x = linelength > >> > end if > >> > ctr = ctr + 1 > >> > Loop > >> > >> > 'Return the file's line count > >> > >> > msgbox "File name " & InputFile & vbcrlf &_ > >> > "FileLineCount " & Ctr & vbcrlf &_ > >> > "Maximum Line length " & x > >> > >> > 'Cleanup > >> > MyFileContents.Close > >> > Set MyFileContents = Nothing > >> > Set fso = Nothing > >> > -------- > >> > TIA > >> > prebble > > Here is my contestant for the quickest/simplest file line counter ... > > > > Function LineCount(sFilespec) > > Const ForAppending = 8 > > with CreateObject("Scripting.FileSystemObject") > > LineCount = .OpenTextFile(sFilespec, ForAppending).Line > > end with > > end Function > > > > I posted an example based on this technique a few days ago. And as I > > recall, someone else showed it to me a few years back. I am told that > > it is off by one count for empty files and files full of blank lines > > (new line characters only - no other text). > As with most things in life, perhaps we need to define what a line is (for > counting purposes) before we can count them. If a file can contain more > than one empty line, then an empty file must contain one empty line. And > perhaps the definition of a line separator is necessary, such as vbLf. With > these definitions, the following script shows that your function produces > correct results; the script is attached also to avoid line wrap problems: > > Option Explicit > MsgBox "started in " & vbCrLf & _ > WScript.CreateObject("WScript.Shell").CurrentDirectory > > fOverWrite "dummy.txt", "" > MsgBox "Line count for empty file -- should be one (for one empty line): " & > LineCount("dummy.txt") > fOverWrite "dummy.txt", vbCrLf > MsgBox "Line count for CrLf file -- should be two (for two empty lines): " & > LineCount("dummy.txt") > fOverWrite "dummy.txt", "Now is the time for all ..." > MsgBox "Line count for one-line file -- should be one: " & > LineCount("dummy.txt") > fOverWrite "dummy.txt", "Now is the" & vbCrLf & " time for all ..." > MsgBox "Line count for two-line file -- should be two: " & > LineCount("dummy.txt") > fOverWrite "dummy.txt", "Now is the" & vbCrLf & vbCr & " time for all ..." > MsgBox "Line count for two-line file -- should be two: " & > LineCount("dummy.txt") > fOverWrite "dummy.txt", "Now is the" & vbCrLf & vbLf & " time for all ..." > MsgBox "Line count for three-line file -- should be three: " & > LineCount("dummy.txt") > > Function LineCount(sFilespec) > Const ForAppending = 8 > with CreateObject("Scripting.FileSystemObject") > LineCount = .OpenTextFile(sFilespec, ForAppending).Line > end with > end Function > > Sub fOverWrite(FilePath, sData) > 'Try writing ASCII file. > 'If error occurs, try writing Unicode file > Dim objFSO 'Global file system object > Set objFSO = CreateObject("Scripting.FileSystemObject") > Dim oTS > Set oTS = objFSO.OpenTextFile(FilePath, 2, true) > On Error Resume Next > oTS.Write sData > If Err then > On Error GoTo 0 > oTS.close > Set oTS = objFSO.OpenTextFile(FilePath, 2, true, -1) > On Error Resume Next > oTS.Write sData > If Err Then > msgbox "Error in fOverWrite - tried ANSI and Unicode" & vbcrlf & _ > "FilePath = " & FilePath & vbcrlf & _ > "sData = " & sData > wscript.quit > End If > End If > On Error GoTo 0 > oTS.Close > End Sub > > -Paul Randall > > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Nifty line counter needed Quote: > > Here is my contestant for the quickest/simplest file line counter ... > > Function LineCount(sFilespec) > Const ForAppending = 8 > with CreateObject("Scripting.FileSystemObject") > LineCount = .OpenTextFile(sFilespec, ForAppending).Line > end with > end Function > it but I didn't really pay attention. When people start using "With CreateObject" my eyes glaze over. It's too much trouble to decipher what their code is actually doing. ![]() |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Nifty line counter needed "mayayana" <mayaXXyana@xxxxxx> wrote in message news:uWkiGM29IHA.4956@xxxxxx Quote: > Quote: >> >> Here is my contestant for the quickest/simplest file line counter ... >> >> Function LineCount(sFilespec) >> Const ForAppending = 8 >> with CreateObject("Scripting.FileSystemObject") >> LineCount = .OpenTextFile(sFilespec, ForAppending).Line >> end with >> end Function >> > That is a nice method. I remember you posting > it but I didn't really pay attention. When people > start using "With CreateObject" my eyes glaze over. > It's too much trouble to decipher what their code > is actually doing. ![]() /Al |
My System Specs![]() |
| | #8 (permalink) |
| | Al Dunbar wins Gold. Thanks Guys, for all the suggestions. They all work. I didn't do any real benchmarking but here's what it looks like ... Bronze medal for Mayayana. Some of my text files are gigantic datbase migration exports. So bronze for Silver for Soler. Was apparently faster but also implies a read of the data. (Good old DOS.) Al Dunbar wins Gold. I added a Msgbox to the end to display the result and it pops up immediately. Even on the big big files. Thanks again, Prebble |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Al Dunbar wins Gold. On Aug 6, 4:08 am, "prebble" <preb...@xxxxxx> wrote: Quote: > Thanks Guys, for all the suggestions. > They all work. I didn't do any real benchmarking but here's what it looks > like ... > > Bronze medal for Mayayana. > Some of my text files are gigantic datbase migration exports. So bronze for > > Silver for Soler. > Was apparently faster but also implies a read of the data. (Good old DOS.) > > Al Dunbar wins Gold. > I added a Msgbox to the end to display the result and it pops up > immediately. > Even on the big big files. > > Thanks again, > Prebble Al, but do we look that much alike? Tom Lavedas =========== http://members.cox.net/tglbatch/wsh/ |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Nifty line counter needed > I disagree. I can't live WITHOUT WITH! ;-) Quote: > a mostly matter of personal taste... I'm not faulting With. It makes a lot of sense for repeat calls to the same object. The code doesn't have to repeatedly get a reference to that object and the flow of the code is more clear. But there are a few things, like use of colons to glue lines together and extreme "With" that, while good for writing terse code, make it difficult to follow what the code's doing. Tom's line counting code is great but the way it's written "hides" Textstream. For someone with experience it's a bit confusing and needs to be decrypted. For someone who's not so familiar with Textstream it's downright confounding. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| The CPU counter is always at 50% | General Discussion | |||
| Nifty Contacts Import Tool! | Live Mail | |||
| more characters in a line needed | Vista mail | |||
| Printer page counter needed | Vista General | |||
| Counter problem | Vista mail | |||