3

I have registered a URL protocol in my system using below script to launch a batch file "showPath.bat".

@echo off
reg add HKEY_CLASSES_ROOT\ProtoTest /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open\command /t REG_SZ /d "C:\TestFolder\showPath.bat" /f

pause

Content of "showPath.bat" is just to display the current working directory. ie.,

@echo off
SET var=%cd%
ECHO %var%
pause

If I run the batch file directly by double clicking it, I can see its path correctly. However if I launch the batch file using the URL protocol registered above. ie, from Chrome, browsing "ProtoTest://", the batch file runs, however display's the path "C:\Windows\system32" instead of the batch file's directory. So, I believe applications launched using URL protocol runs with system32 as working directory. Now How can I get the batch file run from its own directory when launched from browser using URL protocol - without modifying the batch file itself. Only URL protocol possible to be changed from my end.

user1066231
  • 551
  • 1
  • 7
  • 27
  • `%CD%` returns the current working directory, which is something different than the parent directory of the batch script, which you can retrieve using `%~dp0` (including a trailing backslash)… – aschipfl Jul 29 '21 at 14:56
  • @aschipfl thanks, but how can I specify working directory in URL protocol, so that it runs from its own directory. and not from system32. – user1066231 Jul 29 '21 at 14:59
  • `cd /d "%~dp0` before. – Stephan Jul 29 '21 at 15:20
  • @Stephan I cant modify the batch file(3rd party), how to update URL Protocol so, that it runs by using right working directory. The whole example i have given above is for understanding purpose. – user1066231 Jul 29 '21 at 15:54
  • 1
    Hm… what about this: `reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open\command /t REG_EXPAND_SZ /d "%ComSpec% /C \"cd /D C:\TestFolder ^& call showPath.bat\"" /f` – aschipfl Jul 30 '21 at 07:49
  • @aschipfl Wow.. Thanks, thats exactly what I was looking for. Please post this as as answer and I will mark it as answer. I had to surround the path with "" along with escape char. So please mention this which might be helpful for others.. ie, ``` reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open\command /t REG_EXPAND_SZ /d "%ComSpec% /C \"cd /D \"C:\TestFolder\" ^& call showPath.bat\"" /f ``` – user1066231 Jul 30 '21 at 09:07
  • You're welcome! Not sure if the escaping `^&` is really necessary, or if `&` would also work; also `call` might be optional in this particular case… – aschipfl Jul 30 '21 at 09:25
  • @aschipfl seems ^& is needed. without it I am getting error. but as you mentioned call is optional. – user1066231 Jul 30 '21 at 11:02
  • This is strange, I would expect the opposite about the `&`, because the executed command line is going to be `%ComSpec% /C "cd /D C:\TestFolder & call showPath.bat"`, and even if you quoted `"C:\TestFolder"`, the `&` appears within a pair of quotes, so escaping should not be necessary, it should even make the command line fail, because I'd expect `cd` to try to change to a folder called `C:\TestFolder & call showPath.bat`, which doesn't exist, of course. Are you sure the batch file actually runs with escaped `^&`? what is the exact command line you've tried? – aschipfl Jul 30 '21 at 13:38
  • @aschipfl not sure how to add more info in the comment, so I posted it as Answer below. – user1066231 Jul 30 '21 at 14:21
  • 2
    Thanks for the answer, now it is clear to me why it worked, seems I forgot about the outermost pair of quotes in the `reg add` command line; you could also use type `REG_SZ` since `%ComSpec%` is already expanded by the batch file (since the Registry key contains the literal path to `cmd.exe`)… – aschipfl Jul 30 '21 at 17:27

1 Answers1

4

Below code worked for me. I had my batch file in a folder that had spaces in it, so added "" along with escape char. However it gets added to registry as below in image without escape characters. Answer credit to @aschipfl

@echo off
reg add HKEY_CLASSES_ROOT\ProtoTest2 /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2 /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open\command /t REG_EXPAND_SZ /d "%ComSpec% /C \"cd /D \"C:\Source\For Ref\URL Protocol\BatchTest\" ^& showPath.bat\"" /f

pause

enter image description here

user1066231
  • 551
  • 1
  • 7
  • 27
  • Is it possible to close the cmd window after launching the program? – Beetee May 18 '22 at 15:22
  • Is this working in Windows 10 or Windows 7? Now I tested it in my Windows 10 machine, but it failed. – OfusJK Aug 09 '22 at 14:22
  • If I run it in windows 10, It ask a popup like this: https://i.imgur.com/fgCXItH.png. Is there any pre step to enable this fuctionality? – OfusJK Aug 09 '22 at 14:43