Let's say I have the following in my AutoRun
script:
@doskey cd = @( ^
for /f usebackq^^ delims^^=^^ eol^^= %%a in (^
'$* ' ^
) do @( ^
if "%%~a"==" " ( ^
if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^
chdir /d "%%USERPROFILE%%" ^&^& ^
set "OLDPWD=%%CD%%" ^
) ^
) else if "%%~a"=="- " ( ^
if /i not "%%CD%%"=="%%OLDPWD%%" ( ^
chdir /d "%%OLDPWD%%" ^&^& ^
set "OLDPWD=%%CD%%" ^
) ^
) else ( ^
if /i not "%%CD%%"=="%%~a" ( ^
chdir /d "%%~a" ^&^& ^
set "OLDPWD=%%CD%%" ^
) ^
) ^
) ^
)
This is supposed to emulate the behaviour of POSIX cd
in cmd.exe
, and for the most part it works great:
C:\Users\user>cd C:\
C:\>cd
C:\Users\user>cd -
C:\>
However, all it takes is an incomplete pair of double quotes to make that house of cards fall:
C:\>cd "
2> was unexpected at this time.
Is there a way of sanitizing macro input inline so that it can handle misplaced quotes properly?
I've tried/thought of the following:
- creative use of
for /f
with single quotes - what you can see in the provided example: it doesn't break on a list of arguments during string comparison, but"
is all it takes - turning the whole thing into a compound macro (i.e.
for /l (1, 1, 2)...
combined withset args=,
at the end) - nope, can't enable delayed expansion from command line context - ignore
$*
and just use$1
,$2
etc. and hope for the best - doesn't solve the problem as DOSKEY doesn't care about double quotes when delimiting arguments and not applicable tochdir
anyway when command extensions are enabled, as it does not treat spaces (or anything) as delimiters