# Need to invoke a python script

**URL:** <https://resource.dopus.com/t/need-to-invoke-a-python-script/20559>\
**Category:** Help & Support\
**Created:** [May 1, 2015, 8:50pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559 "2015-05-01T20:50:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 1, 2015, 8:50pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/1 "2015-05-01T20:50:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 1, 2015, 9:55pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/2 "2015-05-01T21:55:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [May 1, 2015, 10:41pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/3 "2015-05-01T22:41:41Z")

</div>

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."

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 1, 2015, 11:34pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/4 "2015-05-01T23:34:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [May 2, 2015, 1:03am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/5 "2015-05-02T01:03:14Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [May 2, 2015, 6:26am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/6 "2015-05-02T06:26:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 8:59am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/7 "2015-05-02T08:59:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 9:10am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/8 "2015-05-02T09:10:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 11:02am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/9 "2015-05-02T11:02:00Z")

</div>

I wish I was clever enough to understand this!

> [@Variables and DOS windows](https://resource.dopus.com/t/variables-and-dos-windows/4252/1):
>
> Admin note: This discussion was original in the Buttons & Toolbars thread "[Encode/Transcode selected WAV/MP3 files to MP3 using LAME](https://resource.dopus.com/t/encode-transcode-selected-wav-mp3-files-to-mp3-using-lame/2850/1)". I have split it into a separate thread, below. Once the discussion results in a finished button please post it in…

It does look relevant though.

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

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 11:05am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/10 "2015-05-02T11:05:09Z")

</div>

Here's another topic of interest:

> [@Variable in DOS and SET command conflict](https://resource.dopus.com/t/variable-in-dos-and-set-command-conflict/9648/1):
>
> Hi, I have problem with this command in Batch mode @nodeselect @nofilenamequoting //@runmode hide @set lang={dlgchooseS|Choisir la langue source|Allemand=0407+Chinois=1004+Japonais=0411+Russe=0419} //@externalonly @runbatch @ECHO off //echo {$l…

this looks closer...

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 11:40am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/11 "2015-05-02T11:40:39Z")

</div>

Just trying this example from help:

[gpsoft.com.au/help/opus11/i ... mmands.htm](https://www.gpsoft.com.au/help/opus11/index.html#!Documents/MSDOS_Batch_commands.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}?

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 11:51am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/12 "2015-05-02T11:51:22Z")

</div>

haha

put

echo on

at the start of the code sequence for the button

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [May 2, 2015, 12:47pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/13 "2015-05-02T12:47:41Z")

</div>

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

![](https://resource.dopus.com/uploads/default/original/3X/a/f/afeffd62f5213b5ad21ff290e3f84cba62048722.png)

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]

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 2:06pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/14 "2015-05-02T14:06:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 2:27pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/15 "2015-05-02T14:27:29Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [May 2, 2015, 4:05pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/16 "2015-05-02T16:05:42Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 2, 2015, 4:47pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/17 "2015-05-02T16:47:17Z")

</div>

that's great - thank you very much

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [May 2, 2015, 11:13pm UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/18 "2015-05-02T23:13:30Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [May 3, 2015, 7:17am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/19 "2015-05-03T07:17:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![boogy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@boogy](https://resource.dopus.com/u/boogy)\
**Post date:** [June 3, 2015, 11:51am UTC](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559/20 "2015-06-03T11:51:09Z")

</div>

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.

[Next page](https://resource.dopus.com/t/need-to-invoke-a-python-script/20559.md?page=2)
