Reputation: 15
I just wrote a quick batchfile that would upload a certain directory input into the phone config without the user having to do it manually by hand.
@echo off
set /p title= Contact name?
set /p number= Contact phone number?
start "" http://admin:[email protected]/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%title%)(e164=%number%)(h323=%title%)
The batch file gets the variables and then I add them into the start command. The script as such works and it logs into the phone interface and then uploads the directory input.
If I input John as title and 123456 as the number I get a new browser window with this link http://172.31.2.214/!mod%20cmd%20FLASHDIR0%20add-item%20104%20(cn=John)(e164=123456)(h323=John) .
The batch file works and the user can input directory entry's using this batch. But the problem is that most titles consist of more than one word. The problem is that if I input John Smith under the first variable the link looks like this: admin:[email protected]/!mod%20cmd%20FLASHDIR0%20add-item%20104%20(cn=John
The second part (after the space in the title variable) gets dropped. What could be done to force the batch file to send the URL with a %20 instead of a space character?
Upvotes: 1
Views: 643
Reputation: 6042
In URL encoding spaces are being represented as +
: 123 456
becomes 123+456
(In a URL, should spaces be encoded using %20 or +?). So what you need is to replace spaces in the given string and use +
instead. This is how you can do it:
@echo off
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=%title: =+%
set urlnumber=%number: =+%
start "" http://admin:[email protected]/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%title%)
If you want to encode spaces as %20
instead, here is your code:
@echo off
setlocal EnableDelayedExpansion
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=!title: =%%20!
set urlnumber=!number: =%%20!
start "" http://admin:[email protected]/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%title%)
Upvotes: 1
Reputation: 504
You can replace a string with another string in a variable with %variable:old=new%
. That way, to replace spaces with something else, you can use %variable: =somethingelse%
.
However, the problem is you are trying to replace with %20
, which holds a % sign in it. That will be interpreted as a variable name. If you really want to use %20
and not +
, you will have to use delayed expansion to escape percent signs as explained here :
@echo off
setlocal EnableDelayedExpansion
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=!title: =%20!
set urlnumber=!number: =%20!
setlocal DisableDelayedExpansion
start "" http://admin:[email protected]/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%urltitle%)
Upvotes: 1