batch-filedirectorydoskey

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


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%


Solution

  • 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%%