Need to invoke a python script

Adding..
Set foldertoscan=%foldertoscan:&=^&%

does not work?

hi tbone

That was the next thing I tried, but folderscan only contains up to the ampersand from the previous failed set folderscan={f}.

So in my example:
{f} contains Bert Bacharach - Frankie & Johnny

set folderscan=Bert Bacharach - Frankie & Johnny
causes folderscan to be Bert Bacharach - Frankie

then any subsequent substitution will not work...

It's a tricky one...

If the Function drop-down is set to MS-DOS Batch Function, Opus will automatically escape the & in any filenames as ^& when inserting them into codes like {f}.

Just checked, and the function drop down is already set as you describe i.e. ms-dos batch function, but there's no sign of any escaping as you describe. Could one of the other parameters be affecting this?

Just for completeness, here's the current function:

// default seems to be off
echo on
// the commented stuff here is from the mini tutorial
//cd {sourcepath}
// comment
//dir /w > "{destpath}{sourcepath|nopath|noterm}.txt"
//
// prevent dopus from putting quotation marks around anything with spaces
@nofilenamequoting
// in case more than one folder is selected, just use the first one
@firstfileonly
@externalonly
Set foldertoscan={f}
//Set foldertoscan=%{f}:&=^&%
// insert escape char \ before \ space - '
Set foldertoscan=%foldertoscan:\=\\%
Set foldertoscan=%foldertoscan: =\ %
Set foldertoscan=%foldertoscan:-=\-%
Set foldertoscan=%foldertoscan:'=\'%
Set foldertoscan=%foldertoscan:+=\+%
Set foldertoscan=%foldertoscan:!=\!%
Set foldertoscan=%foldertoscan:&=^&%
CD /D "C:\Users\xxx\Downloads\SonospyReleases\sonospy-current\sonospy"
prompt $D_$T_$G
echo -------------start scan-------------- >>nrscan3.log
C:\Python27\python.exe scan.py -d sonospy.db %foldertoscan% >>nrscan3.log 2>&1
echo ------------end scan--------------- >>nrscan3.log
// use notepad to leave a window with results, so can comment out pause
// notepad logs\scanlog.txt
// start "check database scan log" cmd /k type logs\scanlog.txt
type logs\scanlog.txt
// temporary pause at end of window to check
pause

and here's the output from running against a folder with ampersand in it, showing it all goes pear shaped on the very first line, set folderscan={f}:


C:\Users\xxx>Set foldertoscan=\\AS-304T-FC68\sonos1\E\ERIC KLOSS\Eric Kloss -
Eric Kloss   & The Rhythm Section\
'The' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\xxx>Set foldertoscan=\\\\AS-304T-FC68\\sonos1\\E\\ERIC KLOSS\\Eric Kl
oss - Eric Kloss

C:\Users\xxx>Set foldertoscan=\\\\AS-304T-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric\
Kloss\ -\ Eric\ Kloss\

C:\Users\xxx>Set foldertoscan=\\\\AS\-304T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric
\ Kloss\ \-\ Eric\ Kloss\

C:\Users\xxx>Set foldertoscan=\\\\AS\-304T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric
\ Kloss\ \-\ Eric\ Kloss\

C:\Users\xxx>Set foldertoscan=\\\\AS\-304T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric
\ Kloss\ \-\ Eric\ Kloss\

C:\Users\xxx>Set foldertoscan=\\\\AS\-304T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric
\ Kloss\ \-\ Eric\ Kloss\

C:\Users\xxx>Set foldertoscan=\\\\AS\-304T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric
\ Kloss\ \-\ Eric\ Kloss\

C:\Users\xxx>CD /D "C:\Users\xxx\Downloads\SonospyReleases\sonospy-current\s
onospy"

C:\Users\xxx\Downloads\SonospyReleases\sonospy-current\sonospy>prompt $D_$T_$G


03/06/2015_14:55:26.14_>echo -------------start scan--------------  1>>nrscan3.l
og

03/06/2015_14:55:26.15_>C:\Python27\python.exe scan.py -d sonospy.db \\\\AS\-304
T\-FC68\\sonos1\\E\\ERIC\ KLOSS\\Eric\ Kloss\ \-\ Eric\ Kloss\   1>>nrscan3.log
2>&1

03/06/2015_14:55:26.44_>echo ------------end scan---------------  1>>nrscan3.log


03/06/2015_14:55:26.45_>type logs\scanlog.txt
The system cannot find the file specified.

03/06/2015_14:55:26.46_>pause
Press any key to continue . . .



my version is 11.13

I just tried echo {f} as the first line in the script to eliminate any effect of other commands and had the usual response regarding ampersand. In my experiments I haven't seen any sign of the ^ being used to escape & when the Function drop down is set to MS-DOS batch function. Am I doing something wrong?

The caret to escape the & may be eaten during first use of the variable. You'd need to re-escape on every use maybe, but you may like to try the following. Wrap the initial assignment into quotes, it's good advice to use quotes around paths anyway and your batch does not seem to have them yet at the time %folderscan% is used with python.exe.

Set foldertoscan="{f}"

I don't recall why @nofilenamequoting is used, but maybe removing that helps here too. What issue comes up after that I don't know unfortunately o).

In case you cannot go with the quotes in the variable (maybe because the python.exe has some uncommon quotes handling), then try to remove the quotes again in the very moment the variable is to be passed to python.exe, by placing %foldertoscan:"=% instead of %foldertoscan% there.

I go back to what I said near the start of the thread:

After going down this paths many times I've learned to start with VBScript, even if the task seems simple, and skip trying to do anything with DOS batch, since DOS batch is arcane madness from the 1970s when filenames were 8.3, all-uppercase, and could only consist of a few characters. :slight_smile:

You can put VBScript (or JScript) code directly into a button, and it might be easier to switch to that than to fight with DOS, although there is a bit of a learning curve if you've not used it before (but only a bit).

I'm happy to give vbscript a go - do you have a recommended approach to get going, given that the batch script is a reasonable spec of what the requirement is. I'd like to set up a new button so that I don't lose my existing clunky but by golly working facility that I use lots of times every day.

One thing I need to check - will I be able to generate the final line that runs the python script with the variable containing the directory?

..and just a bit of feedback for tbone...

just to confirm that you are spot on with your last suggestion. I tried it out in a dos command window first.

step 1 capture the folder with possible ampersand with the powerful double quote at each end
set folderscan="{f}"

step 2 put the caret before each ampersand before using it
set folderscan=%folderscan:&=^&%

step 3 substitute in removing the double quote but with the safety of the recently inserted caret
python blah blah blah %folderscan:"=%

This wonderfully arcane mumbo jumbo has worked ok so far...

Thanks tbone!

Good! o) But check if you can get along without removing the doublequotes, it should be more reliable with them.
I would expect the python.exe commandline to get in trouble when there are blanks in your foldername and there are no double quotes present.

I did try the python script with double quotes but no luck. However, I still have the other lines of code in to change space into backslash space etc as required by python. I've probably tried the revised script out about 20 times now and so far, so good...

Just starting to hack my way into vbscript. Outside of dopus I created a helloworld.vbs file containing:

wscript.echo "hello world"

which works fine.

I then created a button, set it to function=script function, and the same line of code as above but this gets an error message:

Error at line 1, position 1
Object required: 'wscript' (0x800a01a8)

I've found a couple of commands that might be useful to invoke python:

set oShell=Wscript.CreateObject("Wscript.shell")
oShell.run="cmd python script etc"

so all I have to do now is work out how to get {f} into a variable and change space into backslash space etc.

Had a look at a couple of vbscript sites but nothing of any use so far. I'm looking for a few simple working dopus vbscripts that I can understand... or fragments that I can use. Learning a language by attempting to swallow the dictionary and the grammar rules is beyond me.

Starting points for scripting in Opus:

Opus manual: Scripting chapter
Opus manual: Scripting reference
Opus forum: Scripting questions & answers
Opus forum: Lots of example scripts

In a script function, clickData.Func.SourceTab.selected (or selected_files or selected_dirs) will get you a list of the selected files.

The simple script function example in the manual shows how to enumerate through a list of files. (It goes through the SourceTab.files list of all files, which isn't the list you want, but the way you enumerate the lists is the same for the selected file/dir lists.)

You can use Opus's own Command object to run the python script, but you can also use WScript's shell.run as well; it doesn't make much difference in this case. (Sometimes one is preferable, sometimes the other, and often it doesn't matter which.)

Well, I've given it a couple of hours, looking through your suggested reading - thanks! I've got the CLI up and running. I've got a button with the example script (copy and pasted in) to display the largest file but this falls over expecting semicolon:

Successfully initialized 'JScript' engine
Error at line 4, position 10
Function OnClick(clickData) {
^
Expected ';' (0x800a03ec)
Parse error - script aborted

I've even tried to write out the script in longhand to understand it but it isn't making a lot of sense. I'll have a look for one of those online courses or a book where you can try things out in the interpreter line by line and build up slowly.

Any suggestions?

Either way this for me looks like it is going to need a big commitment. I can see the logic in someone who knows vbscript or jscript and can jump right in here, but for the layman it is looking quite impenetrable at the moment.

But I haven't thrown in the towel yet - just need another approach - let's call it janet and john...

You'll kick yourself over this one :slight_smile:

JScript keywords are case-sensitive. It needs to be function with a lower-case 'f'.

You also cannot use OnClick(clickData) in the CLI (unfortunately).. o)
U need to create a script button and place the OnClick(clickData) function there to make it work.

ok thanks both for your encouragement. It looks like some "intelligence" was involved in my copy and paste that resulted in most commands, null, etc ending up with upper case but not everything... Anyway after a few iterations through I have a very neat button that finds the largest file in a folder and lets me select it. I just have to dissect this now and try to work out how it does that.

Selecting the largest file is your exercise or demo code you put together for learning I assume?
Because there also is "Select SIZE=largest" to do that, in case I'm wrong. o)