Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to call a batch file with a command I want it to execute. This command contains the %cd% pseudo-variable, which expands to the current directory. Say, I want to run test "echo %cd%". The problem is that %cd% expands before test is called.

Normally, you can escape a variable by doubling the percent signs, but this does not seem to apply to pseudo-variables. For instance, echo %%ProgramFiles%% will print %ProgramFiles%, but echo %%errorlevel%% will print %0%. What do I do?

share|improve this question

The general-purpose escape character in Windows Command Prompt — the analog of \ in the rest of the universe — is caret (^).  Try “^%cd^%”.


… and then,

echo %1 > temp.bat
call temp.bat
del temp.bat

Yes, it’s terribly ugly, and there really ought to be a better way.  Well, I can’t find it.

share|improve this answer
Ah, but now test "echo ^%cd^%" prints %cd% instead of the current directory. What I need is for the pseudo-variable to be preserved when I pass it to the script, but expanded inside. – Don Reba Dec 4 '12 at 1:27
OK, I’ve edited my answer to address that problem. – Scott Dec 4 '12 at 3:18

I'm not having much luck without getting CALL involved, either. With this test.bat:


invoking test.bat call echo ^%cd^% works as expected. Or, putting the CALL inside test.bat:

@CALL %*

and invoking test.bat echo ^%cd^% works. (This is safer anyhow, since if test.bat is getting called from another script, then passing a script to it won't run the risk of exiting prematurely.)

I suppose which works better for you (or if either can be made to work at all) depends on the specifics of your scenario.

share|improve this answer
Oh ye gods, this is a year and a half old? I have got to figure out how the "Related" bar works before I make more of an ass of myself... – Ben Nesson Jun 17 '14 at 19:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.