0

I want to create a fixed pwd alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:

@echo off

doskey ls=dir
doskey pwd=echo ^%cd^%

Running pwd command now prints:

ECHO is on.

I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?

Elgirhath
  • 409
  • 1
  • 7
  • 15
  • 3
    `doskey pwd=cd`. The `cd` command without parameters will just print the current working folder. – Stephan Nov 20 '18 at 10:33
  • 1
    Stephan's solution is the easiest one for command prompt and in a batch file. `doskey pwd=echo ^%cd^%` is the right syntax on `pwd` definition from within a command prompt window for `echo %cd%`. In a batch file must be used `doskey pwd=echo %%cd%%` to define execution of `echo %cd%` on typing `pwd`. Please note that `echo %cd%` as alias for `pwd` is in general not good in case of current directory contains in path an ampersand because of everything after `&` is interpreted by `cmd.exe` as additional command to execute. So best is definitely `doskey pwd=cd`. – Mofi Nov 20 '18 at 10:46
  • @Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the *right-hand side* of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%". – Eryk Sun Nov 20 '18 at 23:07
  • @eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you. – Mofi Nov 21 '18 at 09:21

1 Answers1

3

The links you provided show how to do it.

I have my doskey macros in a file aliases.txt in my folder %USERPROFILE%

11:24:16 C:\Users\LotPings________________________________________
> type Aliases.txt
~=CD /D "C:\Users\LotPings"
\=CD \
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL

And an autorun entry which loads this file

> reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v autorun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    autorun    REG_SZ    Doskey /MacroFile="C:\Users\LotPings\Aliases.txt"

To generate this Autorun automatically copy the following lines into cmd line or a batch file

Set "Key=HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile=\"%USERPROFILE%\Aliases.txt\""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
  • 1
    Note that the C runtime `system` function oddly runs commands via `cmd /c` without the `/d` option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if `%CMDCMDLINE%` indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g. `set "CMD_AUTORUN=%0"`). – Eryk Sun Nov 20 '18 at 22:54