Unable to execute batch file with spaces in arguments

Moderator: leecollings

Post Reply
quack3d
Posts: 86
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Unable to execute batch file with spaces in arguments

Post by quack3d »

I'm running Domoticz in Windows and try the following code to send Pushbullet notification to multiple users:

Code: Select all

    local title = 'this '
    local message = 'works'
    local tit_msg = title..message
    local path = [["C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" ]]
    local combined = path..tit_msg
    os.execute(combined)   
prints will show this: LUA: "C:\Program Files (x86)\Domoticz\scripts\bash\pushbullet.bat" this works

Code: Select all

    local title2 = '"this does" '
    local message2 = '"not"'
    local tit_msg2 = title2..message2
    local path2 = [[& 'C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat' ]]
    local combined2 = path2..tit_msg2
    os.execute(combined2)
print will show this: LUA: & 'C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat' "this does" "not"

Code: Select all

    local title3 = [['"this does" ]]
    local message3 = [["not"']]
    local tit_msg3 = title3..message3
    local path3 = [["C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" ]]
    local combined3 = path3..tit_msg3
    os.execute(combined3)
print will show this: LUA: "C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" '"this does" "not"'

The pushbullet.bat takes two argument and if arguments are surrounded by " ", it will also work with spaces. And copy pasting either of these two into PowerShell works:

Code: Select all

& 'C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat' "this does" "not"

Code: Select all

start "C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" '"this does" "not"'
Suggestions?
Last edited by quack3d on Friday 02 February 2018 10:33, edited 1 time in total.
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by jannl »

Not sure what you mean. It makes sense the argument with spaces does not work. Every parameter in a bash call is seperated with spaces. Counts for every programming language.

Why not use the quotes?
quack3d
Posts: 86
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by quack3d »

Yeah, I know. I guess I wasn't clear enough. I can't get any of those to work in LUA in Domoticz. The print looks right, but it won't send a notification when using quotes and two words. The two lines I wrote at the bottom works only if I paste it in PowerShell.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by emme »

Code: Select all

local title3 = 'this does'
local message3 = 'not'
local tit_msg3 = title3..' '..message3
local path3 = 'C:\\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat'
local combined3 = path3..' "'..tit_msg3..'"'
os.execute(combined3)
can you try this?
...could you explain also why you use the [[ ]] ?
ciao
M
The most dangerous phrase in any language is:
"We always done this way"
quack3d
Posts: 86
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by quack3d »

I assume you meant \\ for every \ like this: 'C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat'

The print for that became LUA: C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat "this does not"

Quotes around the whole "this does not" doesn't look correct and It didn't send any notification either.

I used [[ ]] because I saw it in other examples. :D
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by emme »

about the \\ you're right

I'm a bit unclear about the right syntax for the command....
let's forget for a while LUA and domoticz... on a simple commandline how the bat will be lounched?

like
pushbullet.bat This is not
pushbullet.bat "This is not"
pushbullet.bat "This" "is" "not"
?

if spaces are the problem you can also try to fix this line:

Code: Select all

local path3 = 'C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat'
local combined3 = '"'..path3..' '..tit_msg3..' "'
this should print
"C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat This is not"

but I also in a doubt that windows should be like

Code: Select all

local path3 = 'C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat'
local combined3 = '"'..path3..'" '..tit_msg3
"C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" This is not
The most dangerous phrase in any language is:
"We always done this way"
quack3d
Posts: 86
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Unable to execute batch file with spaces in arguments

Post by quack3d »

the .bat needs to be launched like this: .\pushbullet.bat "this does" "not"

The words 'this does not' was just to illustrate that it didn't work. Basically the batch takes two arguments that allows spaces. The first argument will be shown as title and the second as message. Like this: .\pushbullet.bat "this is the title of the push" "and this is the message body"

But even when I get the print to look correctly, the notification doesn't work.

But now I finally made it:

Code: Select all

    local title7 = 'the title of the notification'
    local message7 = 'the message body of the notification'
    local tit_msg7 = title7..'"' .. ' "' ..message7
    local path7 = '""C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat"'
    local combined7 = path7..' "'..tit_msg7..'""'
    os.execute(combined7)
That prints this: LUA: ""C:\Program Files (x86)\Domoticz\scripts\batch\pushbullet.bat" "the title of the notification" "the message body of the notification""

Now I replaced the message with otherdevices[text_sensor]. Great success! :D

Here is the code for the pushbullet.bat in case anyone else wants to use it:

Code: Select all

@echo off
set api=<type your Pushbullet API key here>
set title=%~1
set body=%~2
curl -u %api%: https://api.pushbullet.com/v2/pushes -d type=note -d title="%title%" -d body="%body%"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest