GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Text file line count

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.

c /dupdata/Dopusdata/ in my last post.

You may want to add a spacer to your toolbar (in customize mode right click the toolbar=>New=>Spacer).

[quote="boogy"]but where is the location bar....???[/quote]It's the field where you can see the path of the current folder. Simply paste the path there. /home (DOpus installation path) and /dopusdata (DOpus configuration path) are Aliases.

Thanks kundal and tbone.

I've moved my button on to the top row immediately to the right of the file etc menu where it stands out.

I found that F4 selects the location bar and /dopusdata works great. There's also a shift-enter that does something similar to F4 but I'm not sure of the purpose of that yet.

Just to experiment I'm going to see if I can add two new buttons: one to generate F4 for the location bar and one to select /dopusdata.

I expect there are a few useful buttons to discover and add.

You can install scripts using drag & drop, without worrying about most of this.

If you have the script saved in a .vbs file, just drag it to the list in Preferences / Toolbars / Scripts and Opus will install it.

The only time you need to go to /dopusdata/Script AddIns manually is if you want to delete a script you have previously installed.

Oops, sorry for creating confusion! /home was indeed the wrong, I meant /dopusdata of course, but you learned a new alias that way, right? o)

@boogy
With location bar/path bar/path field or something, I refer to the textbox or horizontal field, where your current filesystem path is shown. It can be used to enter new paths as well and is an important lister element one cannot oversee, I'd think at least! o))

To move a button, right click a free space on any toolbar and select "Customize", then drag that button around by mouse and after that simply close the "Customize" window to get back to normal working state. Under Prefs -> Toolbars -> Options is an option "Alt-Click to edit Toolbar buttons", if enabled it allows to enter "Customize" mode on pressing ALT and LEFT-clicking any toolbarbutton - this is quicker to get there sometimes.

Just turned this on with customize minimised which is really useful - thanks.

I haven't had any luck searching for help on how to invoke F4 and then enter something but I think its because I need to look for vbscript stuff...

The location bar has F4 and Alt+D as hotkeys by default. You shouldn't need to set up an F4 hotkey for it, unless you've gone and deleted things from the default toolbars.

If you need to add a hotkey to anything on a toolbar, go into Customize mode, right-click the item you're interested in, choose Edit, and use the Hotkey field.