Erfan
Erfan

Reputation: 349

echo %cd% alias does not respond as it should

I have created and alias named pwd for echo %cd% (@DOSKEY pwd=echo %cd%). I have saved it in bat file and configured it to autorun with command processor.

Now whenever I run the command pwd in my command processor it returns with the C:\windows\system32 no matter in which path I am currently in. whereas when I run the echo %cd% it returns the right path I am in.

How do I solve this problem? Is it because of the parameter I am passing to echo? This parameter should update according to the path I am going in. It seems it updates just once when the command prompt is loaded with aliases.

Upvotes: 1

Views: 1782

Answers (1)

dbenham
dbenham

Reputation: 130809

That is because %cd% is expanded during the definition of the macro, not when it is executed.

From a batch file, you should use:

@DOSKEY pwd=echo %%cd%%

If defining from the command line, the expansion rules are different, so you would need something like:

DOSKEY pwd=echo %^cd%

But there is an even simpler method that works in all cases. The CD command without arguments simply lists the current directory. Just enclose the command in parentheses to prevent arguments from being passed.

@DOSKEY pwd=(cd)

Upvotes: 3

Related Questions