Why %cd% set in doskey alias script returns script path instead of the current directory

235 views Asked by At

I am setting an alias file in %userprofile%\alias.cmd containing basic DOSKEY cmd:

@echo off
DOSKEY ls=dir
DOSKEY clear=cls
DOSKEY pwd=echo %cd%

I am installing alias.cmd in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor, such as described in Aliases in Windows command prompt

And test:

C:\Users\tim>cd workspace

C:\Users\tim\workspace>pwd
C:\Users\tim

C:\Users\tim\workspace>echo %cd%
C:\Users\tim\workspace

I am wondering why %cd% returns script path instead of the current directory doskey pwd should have the same result than echo %cd%

1

There are 1 answers

0
jeb On BEST ANSWER

It's because the line:

DOSKEY pwd=echo %cd%

is inside a batch script, there the %cd% will be evaluated at the time of your definition of the macro. You set your pwd-macro with a fixed value.

Try to use:

DOSKEY pwd=echo %%cd%%