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.
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