Need help with command line string replacement
Posted: 25 May 2019 09:15
Hello,
I'm a bit new to the batch/dos programming world and was wondering if there was a way to do string replacement, at a command prompt, as opposed to inside a batch file?
A little background, a while ago I needed to register a custom URI protocol that would forward the invoked url, to chrome, via a registry hack (https://stackoverflow.com/questions/474 ... ashtag-and ) which resulted in :
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX]
@="Chrome HTML Document"
"URL Protocol"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\DefaultIcon]
@="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe,0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell\open]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell\open\command]
@="cmd /c cd \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & FOR /f \"tokens=1-3delims=?#\" %%a IN (\"%1\") DO (if \"%%a#%%b%%c\"==\"%1\" (chrome.exe %1) else (chrome.exe %%a#%%c%%b) )"
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\ProtocolExecute\CNX]
"WarnOnOpen"=dword:00000000
The problem that I've noticed now, is that via some new windows update, what used to be invoked as cnx:// www.someurl.com in IE, now gets turned into as cnx://%20www.someurl.com when the command executes (i had it spit out what %%a is, and it has the %20 in it).
I know it sounds crazy...but, before applying windows updates to my machine, yesterday, everything was honky dory, with the space not being encoded, and after the updates, i saw it getting the space encoded...so it's gotta be something in a new update.
Therefore, i'm wondering if there's a way i can do a string replace somewhere, and either
strip %20 from the url (which might not be right in the long term, since certain urls are bound to have a space in them, encoded as %20, further in the url string)
or to completely strip out cnx://%20 from my %%a variable (any any of the variables)
but playing with the set command hasn't worked for me.
Not even sure if what I'm trying to do is possible?
Any help would be greatly appreciated!
Edit: I got some help on stack overflow which seems to work perfectly, in case anyone ever needs something like this:
https://stackoverflow.com/questions/563 ... and-prompt
@="cmd.exe /V:ON /C cd /D \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & set \"url=%1\" & set \"url=!url:cnx://%%20=!\" & set \"url=!url:cnx://=!\" & for /F \"tokens=1-3 delims=?#\" %%a in (\"!url!\") do if \"%%a#%%b%%c\"==\"!url!\" (chrome.exe \"!url!\") else (chrome.exe \"%%a#%%c%%b\")"
Thank you for your help, pieh-ejdsch!
I'm a bit new to the batch/dos programming world and was wondering if there was a way to do string replacement, at a command prompt, as opposed to inside a batch file?
A little background, a while ago I needed to register a custom URI protocol that would forward the invoked url, to chrome, via a registry hack (https://stackoverflow.com/questions/474 ... ashtag-and ) which resulted in :
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX]
@="Chrome HTML Document"
"URL Protocol"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\DefaultIcon]
@="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe,0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell\open]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CNX\shell\open\command]
@="cmd /c cd \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & FOR /f \"tokens=1-3delims=?#\" %%a IN (\"%1\") DO (if \"%%a#%%b%%c\"==\"%1\" (chrome.exe %1) else (chrome.exe %%a#%%c%%b) )"
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\ProtocolExecute\CNX]
"WarnOnOpen"=dword:00000000
The problem that I've noticed now, is that via some new windows update, what used to be invoked as cnx:// www.someurl.com in IE, now gets turned into as cnx://%20www.someurl.com when the command executes (i had it spit out what %%a is, and it has the %20 in it).
I know it sounds crazy...but, before applying windows updates to my machine, yesterday, everything was honky dory, with the space not being encoded, and after the updates, i saw it getting the space encoded...so it's gotta be something in a new update.
Therefore, i'm wondering if there's a way i can do a string replace somewhere, and either
strip %20 from the url (which might not be right in the long term, since certain urls are bound to have a space in them, encoded as %20, further in the url string)
or to completely strip out cnx://%20 from my %%a variable (any any of the variables)
but playing with the set command hasn't worked for me.
Not even sure if what I'm trying to do is possible?
Any help would be greatly appreciated!
Edit: I got some help on stack overflow which seems to work perfectly, in case anyone ever needs something like this:
https://stackoverflow.com/questions/563 ... and-prompt
@="cmd.exe /V:ON /C cd /D \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & set \"url=%1\" & set \"url=!url:cnx://%%20=!\" & set \"url=!url:cnx://=!\" & for /F \"tokens=1-3 delims=?#\" %%a in (\"!url!\") do if \"%%a#%%b%%c\"==\"!url!\" (chrome.exe \"!url!\") else (chrome.exe \"%%a#%%c%%b\")"
Thank you for your help, pieh-ejdsch!