Need to invoke a python script

Currently I run a dos batch script which invokes a python script to scan music files and keep a database updated.

I'd like to be able to right click a directory and just have the python script run against that directory, so that I don't have to scan everything for a small change. I'm open to suggestions on any other way to invoke the script...

The current batch script looks like this:

cd C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy python scan.py -d sonospy.db \\\\NAS\\sonos1 >>scan3.log 2>&1

The variable in these two lines is the \\NAS\sonos1 part and each backslash has to be doubled up...

Is this at all feasible?

The variable in these two lines is the \\NAS\sonos1 part and each backslash has to be doubled up, and any space should be preceded by a backslash, so for example:
\\NAS\share1\dir1\this\ directory\ has\ spaces

I'm not sure from the example how you're mapping from the current local directory to the \NAS\sonos1... folder, or if it's always literally \NAS\sonos1 on its own, or something else.

It may be easiest to edit the script you're running so it can do the mapping and/or double-up the \ characters itself. Then everything stays in one place.

If that isn't desirable, you could write a script in Opus which maps and/or modifies the path into the required format before running the python script.

Some concrete examples might clarify what needs to be done. e.g. "I right-click the folder with path X, and it then runs command Y."

Here's another go then:

I right click on a folder with path X and can choose an option (call it Database Scan) which captures the path, and turns it into a variable that the batch file can use, the equivalent of doing:

set scandir=\\nas\share1\directory\ name\ with\ spaces

which would then be used as the variable:

cd C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy python scan.py -d sonospy.db %scandir% >>scan3.log 2>&1

... and you are spot on about where the extra backslashes are inserted - it does make real sense to do it at the start of the batch command file, assuming that it is possible. Lots of permutations I'm sure.

What about this? Using batch string manipulation to double the backslashes and escape the blanks.
Not tested.. the button/context menu entry for "folders" needs to be of type "MS DOS-Batch" I assume.

[code]
@nofilenamequoting
Set foldertoscan={f}
Set foldertoscan=%foldertoscan:=\%
Set foldertoscan=%foldertoscan: =\ %

cd C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy
python scan.py -d sonospy.db "%foldertoscan%" >>scan3.log 2>&1[/code]

I doubt that will work as Set is an Opus command and the untested script won't be running the DOS set command.

It could possibly be made to work with @externalonly (if I've remembered correctly) but I'd always use a script over DOS batch as DOS batch usually falls over in some situations, like certain characters in the paths.

It's always difficult for the novice to know whether something is easy or difficult with opus. I'm sensing this one is not in the easy category.

So, looking up batch in the help, does this sort of approach make sense:

set up a batch function (using the help instructions)
are {sourcepath} or {destpath} available as containing the directory name that was right clicked?

then a bit of dopus stuff to manipulate the directory name as per tbone

then @externalonly to switch off any interpretation of what follows as dopus commands

set folderscan=whatever the manipulated directory name is from dopus (this is the tricky one because it's the bridge between dopus and batch)
cd C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy
python scan.py -d sonospy.db "%foldertoscan%" >>scan3.log 2>&1

I wish I was clever enough to understand this!

It does look relevant though.

Ready to start experimenting I think. Chip in if you have a less vague idea than I have...

Here's another topic of interest:

this looks closer...

Just trying this example from help:

gpsoft.com.au/help/opus11/i ... mmands.htm

How can I make the commands in the batch window visible? I have put a PAUSE command to hold the window open but can't see what commands are running before then.

I'm selecting a folder, then clicking the button I've created to run the batch file, but then I realised I don't know how to find out what {destpath} is. I've changed the script to take that out for now, so I can see that I've created a named text file containing the dir output in the {sourcepath} dir.

Is there a way to use my selected folder in {sourcepath} instead of {sourcepath}?

haha

put

echo on

at the start of the code sequence for the button

This seems to work, and prints out an appropriate line:


So this should work:

Function type: MS-DOS Batch Function

[code]@nofilenamequoting
@externalonly
Set foldertoscan={f}
Set foldertoscan=%foldertoscan:=\%
Set foldertoscan=%foldertoscan: =\ %

CD /D "C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy"
python scan.py -d sonospy.db "%foldertoscan%" >>scan3.log 2>&1[/code]

Thanks Leo, and tbone. Well with the assistance of your tips, the helpfile, that tutorial and a few lucky finds on the forum I was a gnat's whisker away. I didn't realise you could have a dos command like set together with some dopus processing, assuming that:

Set foldertoscan={f}

is the dos command SET FOLDERSCAN= followed by some dopus magic to substitute the currently selected folder.

This is of course absolute magic and much easier to understand than the equivalent in dos batch speak.

So that works and will be used every day.

Thanks!

p.s. I read up on cd /D and I understand it switches drive and path., but despite some research I'm not sure why it is needed, as it has worked without the /D in my hard coded batch file.

If I select more than one folder then click the button, the set command is invoked for each folder in the list and the last one in the list is adjusted for backslashes then carries on, so the last folder selected is processed. This is more out of interest than a real problem.

{f} is the short form of {filepath$} - from help, this is the "need" form of {filepath} (requires at least one selected item in the file list).

Is there a simple way to just stop if multiple items are selected i.e the array count of {f} is not equal to 1?

I think the python scan command I'm using can take in multiple foldertoscan e.g.

python scan.py -d sonospy.db %foldertoscan1% %foldertoscan2% etc >>scan3.log 2>&1

but I'm happy to process single folders - just want to prevent obvious errors if I take the pause away...

@firstfileonly will make it only use the first selected file or folder and ignore any others.

Other selection criteria and logic can be applied using a script button.

that's great - thank you very much

Hooray! Always good to see people trying some things and even reading the docs, the only way to get behind some of DOs best tricks! o)

The /D switch on a CD command sets the current drive as well and not only the current directory on that drive.
You'd need an additional line, reading "C:" or "D:" etc. in the batch, to additionally switch the current drive.

For your example, this did not make a difference, as your windows installation or "dopus.exe" are on the same drive as "scan.py".
I think it's best practice and always a good idea to add the /D to a cd command. It makes it work as I'd expect a "cd" command to work. o)

Thanks for your encouragement. DO is such a versatile tool kit. So much to learn at the university of directory opus...

Hi

The button to invoke the python script is working very well... except for one itsy bitsy teeny weeny problemette.

If there is an ampersand (&) in the folder name e.g. Bert Bacharach - Frankie & Johnny, on the very first Set command (on line 3 below as per the original code) the batch command fails because ampersand indicates there's another batch command. The failure takes place even before the changes to escape characters like backslash and space (as below)

@nofilenamequoting
@externalonly
Set foldertoscan={f}
<<<< batch command fails here with x is not recognised as an internal or external name>>>>
Set foldertoscan=%foldertoscan:=\%
Set foldertoscan=%foldertoscan: =\ %

CD /D "C:\Users\xxxx\Downloads\SonospyReleases\sonospy-current\sonospy"
python scan.py -d sonospy.db "%foldertoscan%" >>scan3.log 2>&1

I've tried enclosing it in quotes and backslash but the ampersand is too powerful. It looks like the correct way to escape it is with the caret, but this needs to be done before we use the set command, so is there a way to manipulate the contents of {f} as a dopus variable type thing...?

So in this example, {f} needs to contain Bert Bacharach - Frankie ^& Johnny before line 3 without using Set.