Text file line count

Forum search didn't find anything and there's nothing obvious in the display attributes, so can I ask how I might be able to see the line count for a text file.

I have viewer and metadata panes open.

Is there a simple way to do this? It could be some sort of button to press for a selected file. Doesn't need to calculate every line count on the off chance that I want it.

I think you would have to use (or write) an external application to do this. One option is to invoke the Windows FIND command from a DOS prompt along these lines..

type myfile.txt | find /v "asdfgh" /c

This will give you a count of the lines that don't contain the string "asdfgh". As long as you pick a sufficiently unlikely string the output will be the number of lines in the text file. Credit to this article for a possible solution.

Regards, AB

The viewer reports the line count for text files at the top of the viewer panel, next to the filename.

(Unless you have disabled the Text viewer plugin.)

Thanks all for your help.

I have my viewer pane top right of the screen and I think the line count is not visible because the filename is truncated with "...", squeezed out by the 11 icons to the right for doing stuff with the image. Yep, just tried a very short filename and the line count is there. I've made the viewer pane a bit wider but it's still hit and miss.

Darn!

Do I have any display options or tooltip type options to make the line count visible if I hover over the viewer pane, or something...?

In the meantime I can try out the dos command (looks good) - would I be able to have a button that knew the name of the text file and I could press it without further updating of details?

Looked at preferences viewer pane and there is an option currently not selected call Show Control Bar, which when selected seems to put the same type of icons currently overcrowding the top row on a new bar at the bottom, whuch is perfect, except that the icons on the top row are still there...

Darn! Close!

Can't see any tooltip options in preferences.

Inspired by this thread I wrote a small Script-Addin, that adds a new column which shows the number of lines for text based files in this list:
.txt .ini .htm .xml .rdf .html .url .shtml .lng .dcf .dop .omd .torrent .php .css .inc .js .tpl .sql .reg .bat .vbs .cmd .btm .ncl .m3u .pls .cue

TextFileLineCount.vbs

Option Explicit

 'TextFileLineCount
' (c) 2014 Kundal
' 
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
' 
' Called by Directory Opus to initialize the script
Function OnInit(initData)
	initData.name = "TextFileLineCount"
	initData.desc = ""
	initData.copyright = "2014 Kundal"
	initData.version = "1.0"
	initData.default_enable = false

	Dim col

	Set col = initData.AddColumn
	col.name = "Lines"
	col.method = "OnLines"
	col.label = "Lines"
	col.justify = "left"
	col.autogroup = true
End Function

' Implement the Lines column
Function OnLines(scriptColData)
	Const ForReading = 1
	Dim objFS
	Dim objTS
	Dim strExt
	Dim strTemp
	
	Set objFS = CreateObject("Scripting.FileSystemObject")
	strExt = ".txt .ini .htm .xml .rdf .html .url .shtml .lng .dcf .dop .omd .torrent .php" &_
	".css.inc .js .tpl .sql .reg .bat .vbs .cmd .btm .ncl .m3u .pls .cue"

	If scriptColData.item.is_dir = False And (instr(strExt, scriptColData.item.ext)) Then
		Set objTS = objFS.OpenTextFile(scriptColData.item)
		Do While objTS.AtEndOfStream <> True
			strTemp = objTS.SkipLine
			scriptColData.value = objTS.Line
		Loop
	End If
End Function

It would probably be slightly better for performance if you used a variable to count the lines and then assigned it to the column value once at the end (otherwise it means an IDispatch call for every line in the file).

Thanks for the hint. Unfortunately I have no clue how to do this because I'm no professional.

Something like this:

' Implement the Lines column
Function OnLines(scriptColData)
   Const ForReading = 1
   Dim objFS
   Dim objTS
   Dim strExt
   Dim lineCount
   
   Set objFS = CreateObject("Scripting.FileSystemObject")
   strExt = ".txt .ini .htm .xml .rdf .html .url .shtml .lng .dcf .dop .omd .torrent .php" &_
   ".css.inc .js .tpl .sql .reg .bat .vbs .cmd .btm .ncl .m3u .pls .cue"

   If scriptColData.item.is_dir = False And (instr(strExt, scriptColData.item.ext)) Then
	  lineCount = 0
      Set objTS = objFS.OpenTextFile(scriptColData.item)
      Do While objTS.AtEndOfStream <> True
         lineCount = lineCount + 1
         objTS.SkipLine
      Loop
      scriptColData.value = lineCount
   End If
End Function

Many thanks for the help! Works great.
Here's the complete script with jon's improvement:

TextFileLineCount.vbs

[code]Option Explicit

'TextFileLineCount
' (c) 2014 Kundal
'
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
'
' Called by Directory Opus to initialize the script
Function OnInit(initData)
initData.name = "TextFileLineCount"
initData.desc = ""
initData.copyright = "2014 Kundal"
initData.version = "1.0"
initData.default_enable = false

Dim col

Set col = initData.AddColumn
col.name = "Lines"
col.method = "OnLines"
col.label = "Lines"
col.justify = "left"
col.autogroup = true
End Function

' Implement the Lines column
Function OnLines(scriptColData)
Const ForReading = 1
Dim objFS
Dim objTS
Dim strExt
Dim strTemp
Dim lineCount

Set objFS = CreateObject("Scripting.FileSystemObject")
strExt = ".txt .ini .htm .xml .rdf .html .url .shtml .lng .dcf .dop .omd .torrent .php" &_
".css.inc .js .tpl .sql .reg .bat .vbs .cmd .btm .ncl .m3u .pls .cue"

If scriptColData.item.is_dir = False And (instr(strExt, scriptColData.item.ext)) Then
lineCount = 0
Set objTS = objFS.OpenTextFile(scriptColData.item)
Do While objTS.AtEndOfStream <> True
lineCount = lineCount + 1
objTS.SkipLine
Loop
scriptColData.value = lineCount
End If
End Function[/code]

Here is a version, based on Kundal's code, that you can run from a button. Sample output...


Script Function button code..

@language vbscript
Option Explicit

Function OnClick(ClickData)
   Dim cdf, cmd, dlg, files, i, n, tab, str
   Set cdf = ClickData.Func
   Set cmd = cdf.command
   Set dlg = cdf.Dlg
   With dlg
      .title = "Line Count"
      .buttons = "OK"
   End With
   str = "" 

   ' Prevent selected items from being deselected
   With cmd
      .deselect=False
   End With
   
   Set tab = DOpus.listers(0).activetab
   ' Only process files, not folders
   Set files = tab.selected_files

   ' Process selected files, if there are any
   For i = 0 To files.count-1
      n = LineCount(files(i))
	  ' Only process eligible files as determined by the LineCount function
	  If Not n = False Then
	     If Not str = "" Then str = str & vbcrlf
		 str = str & files(i).name & " - " & n & " line"
		 If n > 1 Then str = str & "s"
	  End If
   Next

   ' Show line counts for eligible files
   If Not str = "" Then
      With dlg
         .message = str
	     .show
      End With
   End If
   
End Function

Function LineCount(file)

   Dim objFS
   Dim objTS
   Dim strExt
   Dim strTemp
   Dim n
   
   Set objFS = CreateObject("Scripting.FileSystemObject")
   ' Define all eligible file extensions here
   ' Files with non-matching extensions are ignored
   strExt = ".txt .ini .htm .xml .rdf .html .url .shtml .lng .dcf .dop .omd .torrent .php" &_
   ".css.inc .js .tpl .sql .reg .bat .vbs .cmd .btm .ncl .m3u .pls .cue"

   n = False
   If (InStr(LCase(strExt), LCase(file.ext))) Then
      Set objTS = objFS.OpenTextFile(file)
      Do While objTS.AtEndOfStream <> True
         strTemp = objTS.SkipLine
         n = n + 1
      Loop
   End If
   LineCount = n

End Function

Regards, AB

I put a slightly improved version of the column into the add-in section: Column: FileInfo (number of lines, EOL line-endings, encoding of text files, more..)

ReadAll() is faster than ReadLine() and manual counting of lines is not required for both, another minor hint: VBScript is quite tricky at if-statements.
Code likeIf 0 and myString="test" Then .. will always do the string comparison, though the expression evaluates to false right at the start.
This is different from many languages and may result in wasted cpu cycles and weird effects in case you test against function return values.

Many thanks for providing your improved version. I'm always glad to learn something new about scripting.

I wish we had a professional scripting guy like you in our german H&P Community Forum team as moderator. :wink:

I have added yet another tweaked version to the Script Buttons & Addins forum. See [url=https://resource.dopus.com/t/display-the-number-of-lines-in-selected-text-files/18993/1]. Instead of adding line counts in an extra column, it displays them in a pop-up. The new code uses the recently added DOpus MAP object to test for eligible file extensions so you must be using Opus v11.5.1 Beta or later.

Regards, AB

This feels a bit like going down the motorway on a bicycle at 10mph while the ferraris nip up and down at 200mph and occasionally park up, everyone has a quick chat in a language I don't understand and then whizz! off they go again.

I'm going to focus on the latest post from aussieboy and see how I can have a go with this. I like the idea that work to count lines is only carried out when I ask for it. I searched for buttons etc and found this:

on How to add buttons from this forum to your toolbars, so will have a go.

[Aside: is it correct that I can't edit a post once made?]

I'm running the latest beta so that should be ok.

Will this code work when the text file is on a NAS?

We'll soon find out.

Looked for Script Addins folder in c:\Program Files\GPSoftware\Directory Opus but there isn't one.

Not sure if this is the right place for it but that's where I've put it.

And the osp file is now there.

Not sure if I have to restart dopus etc.

Went through the process of creating a button but no clue as to how or what to assign it to.

Anyone speak idiotspeak out there?

Enter "/home" in your location bar and you should get to the place where "script addins" is located. Put your file there, no restart required.

Please read this help page to learn more about script Add-ins: Script Add-ins
The button code for aussieboykies Version is simply $LC. You'll have to choose Function=>Scriptfunktion in the button editor to make it work. Otherwise you'll get an error message.
Leo's article about adding buttons to a toolbar is a little bit outdated when it comes to Script Buttons & Addins.

@tbone: /home is the wrong place. It's /dopusdata/Script AddIns.

Thanks tbone but where is the location bar....???

Anyway in another part of the galaxy I found this:

and it gives me a clue so I dragged and dropped the line count osp file into preferences toolbar scripts (which worked!!) and then created my button in the onkly space there was (to the right of the help button) and then with a flash of inspiration selected advanced and put $LC into the code window.

And this works on the NAS drive and elsewhere.

I found a reference to Script Addins in the dupdata directory so off to find that.
Also need to work out how to move my new LIne Count button so its closer to the action.
Thanks.