Need help with command line string replacement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dupuis2387
Posts: 2
Joined: 25 May 2019 09:03

Need help with command line string replacement

#1 Post by dupuis2387 » 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!
Last edited by dupuis2387 on 28 May 2019 07:57, edited 1 time in total.

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Need help with command line string replacement

#2 Post by pieh-ejdsch » 26 May 2019 11:13

hello,
try this:

Code: Select all

@="cmd /c pushD \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & for /f \"tokens=1,2*delims=:.\" %%i in (\"%1\") do for /f \"tokens=1-3delims=?#\" %%a IN (\"%%i:// www.%%k\") DO (if \"%~x1\"==\".php\" (chrome.exe %%a#%%c?%%b) else (chrome.exe %1) )&popD "

dupuis2387
Posts: 2
Joined: 25 May 2019 09:03

Re: Need help with command line string replacement

#3 Post by dupuis2387 » 27 May 2019 08:28

pieh-ejdsch wrote:
26 May 2019 11:13

Code: Select all

@="cmd /c pushD \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\\" & for /f \"tokens=1,2*delims=:.\" %%i in (\"%1\") do for /f \"tokens=1-3delims=?#\" %%a IN (\"%%i:// www.%%k\") DO (if \"%~x1\"==\".php\" (chrome.exe %%a#%%c?%%b) else (chrome.exe %1) )&popD "
Thanks for that, pieh-ejdsch ! I'm trying to wrap my mind around all of that; can I ask what \"%~x1\" does? when i change it to cmd /k to look at the output, it just has that literal expression. Does it need to be \"%%~x1\"?

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Need help with command line string replacement

#4 Post by Meerkat » 28 May 2019 10:36

Hello!

First of all, I did not try to understand the code :lol: . Sorry .

Upon entering for /? in CMD, you get this part:
Command Prompt wrote:In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
...
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
...
...
%~x1 (and other stuffs above) works even when the file corresponding to %1 does not exist; %1 is merely treated as "file name string". For example, upon entering for %a in ("something.yeah") do echo %~xa in CMD, the output is just
Command Prompt wrote:C:\..\>echo .yeah
.yeah
Meerkat

Post Reply