Re: Question About Quotation Marks
Posted: 05 Mar 2013 22:18
The whole point of the code is to check how the batch was started. Only if it was started ''normally'' i.e. via ShellExecute, double-click in Explorer, or via a command that has to be specifically made to look like those, should the " env var be defined. In other cases, like when run via a command prompt, or from within another batch, the " env var should be undefined.
For fun, this iterative process has been to reduce cases where it all explodes, and explore some of the crazier aspects of cmd.exe's parsing.
Anyhow, I added an incremental poison character replacement to deal with both % and ?.
test35&%%0&;&this.bat
This can be started with:
from a second batch file and not choke.
Edit - For my specific use of call, I found a little trick:
The colon changes the file search to check for call:, which is an invalid file name so it aborts without checking all locations in the search paths (without having to null out the search paths and pathext).
Edit 2 - I went back and retested some earlier breaking filenames, and I rediscovered why I'd used :^&rem; which is to say it does serve a purpose. I'll elaborate when I post my next iteration. I'm also trying to figure out if I can jam it into the double call; an escaped & is no good because of caret doubling.
Queue
For fun, this iterative process has been to reduce cases where it all explodes, and explore some of the crazier aspects of cmd.exe's parsing.
Anyhow, I added an incremental poison character replacement to deal with both % and ?.
test35&%%0&;&this.bat
Code: Select all
@setlocal
@echo off
:: @prompt;::$S$T$_
setlocal enabledelayedexpansion
path; & dpath; & set pathext=;
set ""=^" & set "{=!CMDCMDLINE!"
if not "!{:~10,1!"==":" goto:chk
if not "!{:~-2,1!"==" " goto:chk
(::
:;:!CMDCMDLINE:*:=*!!CMDCMDLINE:~0,1!)
2>nul call call(%%%%CMDCMDLINE:**=*%%0%%%%
set "}=!CMDCMDLINE:^^^^=^^!"
if "!}:~3,1!"==":" set ""=:^"
(::
:;:!CMDCMDLINE:~0,1!)
set "}=!{:/=/0!"
set "}=!}:?=/1!"
2>nul call(:^&%%CMDCMDLINE:**=!}:%%=/2!%%
(::
:;:!CMDCMDLINE:^^^^=^^!!CMDCMDLINE:/2=%%!!CMDCMDLINE:/1=?!!CMDCMDLINE:/0=/!)
if !CMDCMDLINE!==!{! echo CMDCMDLINE restored
:chk
endlocal & if not "%"%"=="" ( set ""="" ) else set ""=^"
echo [%"%]
if not defined ^" goto:jmp
"&"^&\..\test35^&%%%%0^&^;^&this.bat test
:jmp
pause
cls
Code: Select all
cmd /c ^""%~dp0test35&%%%%0&;&this.bat" "/?" ^"
Edit - For my specific use of call, I found a little trick:
Code: Select all
2>nul call call:(%%%%CMDCMDLINE:**=*%%0%%%%
Edit 2 - I went back and retested some earlier breaking filenames, and I rediscovered why I'd used :^&rem; which is to say it does serve a purpose. I'll elaborate when I post my next iteration. I'm also trying to figure out if I can jam it into the double call; an escaped & is no good because of caret doubling.
Queue