Batch file parse phone numbers

177 Views Asked by At

In order to use my softphone I have registered a custom url protocol, cccx

This is then associated with a .bat file that parses the phone and sends the command to the softphone.

The code I use is:

set str=%1  
set str=%str:cccx:=%
set str=%str:+=00%
set str=%str:0030=%
set str=%str:-=%
set str=%str: =%

"C:\Program Files (x86)\3CXPhone\3CXPhone.exe" dial:9%str:cccx:=%

However this is not enough, some web pages send characters differently and I can't parse it.

For example, a phone is shown as +39 02 33919999.

I receive this as either str="%2b39+02+33919999" or str="+39%2002%2033919999"

In both cases this must become 00390233919999

I've tried various ways, but can't seem to handle it correctly.

Thank you for any help

1

There are 1 best solutions below

1
On BEST ANSWER

The % character needs to be escaped by itself when attempting to use substring modification search replace syntax, which in turn requires that the substring modification be executed using delayed expansion.

Then all it takes to handle the cases presented is simple conditional test to work out what search replace actions are required:

@Echo off

Set "input=%~1"

rem test cases: %2b39+02+33919999 +39%2002%2033919999
rem output: 00390233919999

Setlocal EnableDelayedExpansion

 Set "input=!input:+=!"

 If not "!input:2b=!" == "!input!" (
  Set "input=!input:%%2b=00!"
 )Else (
  Set "input=00!input:%%20=!"
 )

 Set input
 Pause