1

So I've set up a file with some aliases for commands that I commonly use. I added it to the registry like in this answer.

I want to use this alias like so:

>cmd /k newalias 'newalias' is not recognized as an internal or external command, operable program or batch file. 

So this alias cannot be used. If I type >cmd /k newalias again, now it works, so the problem seems to be that the command is being run before the doskey commands in the alias file are executed.

Is there any way to wait until after these aliases are created before running the command?

2
  • You cannot use an alias as an actual command like you're trying to do. doskey.exe provides a command-line interface for the console API AddConsoleAlias. Aliases are implemented in the console (conhost.exe). They transform the input buffer when an alias matches at the beginning of a line of input. The current set of aliases depends on the foreground process name. It has nothing to do with cmd.exe, except if you have aliases defined for "cmd.exe" and cmd is the current foreground process in the console. Commented May 5, 2015 at 14:01
  • Also, it would be simpler and more efficient to load all of your aliases in one pass using the /macrofile option. Commented May 5, 2015 at 14:02

1 Answer 1

1

strange behaviour, but if you use doskey after you import your macro that is working :

cmd /K "doskey /macrofile=c:\temp\macros.txt & doskey /macros >null & newalias" 

edit the above commant doesnt work, newalias has to be written manually in the console.

Sign up to request clarification or add additional context in comments.

10 Comments

I have newalias defined in macros.txt, but this command just results in the error message 'newalias' is not recognized .... It's not a file that can be found and executed via CreateProcess or ShellExecuteEx, and it's not implemented by the shell like a bash alias.
The alias transforms input in conhost.exe before it's ever read by the client process via ReadFile, ReadConsole, or ReadConsoleInput. This console-based alias system is fairly limited in some ways -- e.g. it can't be treated generically as a command and has to be loaded for every console window. But it's also more powerful in some ways -- e.g. aliases can be defined specifically for any executable such as cmd.exe, powershell.exe, cdb.exe, python.exe, etc (use sections like [cmd.exe] in the macro file).
I'm also using a Windows 7 x64 workstation. What's newalias defined as? I have simply newalias=dir for testing. Maybe you have an actual batch file or executable with the name "newalias". Check where newalias.
I just tried np=notepad++.exe. Try to remove >null to see if that help
I can't fathom how it's working for you (I won't rule it out just yet, but it has me baffled). All that 'works' for me is to execute cmd.exe via CreateProcess and then call WriteConsoleInput to write "newalias\r\n" to the console as a sequence of INPUT_RECORD records. So it's the same as if I had manually entered newalias at the prompt. Using a pipe also doesn't work because that doesn't go through conhost.exe; I knew it wouldn't, but I tried anyway via echo newalias | cmd.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.