Batch file is a double-edged sword, the good side is that batch file runs on almost all of the Microsoft platforms, while the evil side is people just couldn't get it right.
At the highest level, batch file is interpreted by the command processor, which is cmd.exe or command.com. The interpreter is in charge of:
What the command processor does not care are the C/C++ style "argv" parsing and double quotes - it is the responsibility of individual process to parse the command line, although in normal case this is handled by CRT or shell32!CommandLineToArgvW.
Let's jump to today's question - how do you write a batch file that prints out the full path of itself? (hint: %~dpnx0 and %~f0 have bug and won't work if the batch is invoked with double quotes).
I've put my answer as follows:
IF EXIST "%~f0" (
IF NOT "%~x0" == "" (
FOR /F "delims=; tokens=1,*" %%P IN ("!PATH_CDR!") DO (
IF "!PATH_CAR:~-1!" == "\" (
IF NOT "!PATH_CAR!" == "" (
FOR %%X IN (%PATHEXT:;=;%) DO (
IF EXIST "!PATH_CAR!\%~n0%%X" (
IF DEFINED PATH_CDR (
ECHO Error: failed to detect batch file path 1>&2
EXIT /B -1
CALL "!PATH_CAR!" %*
EXIT /B !ERRORLEVEL!
EXIT /B %ERRORLEVEL%