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?

Community
  • 1
  • 1
  • 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`](https://msdn.microsoft.com/en-us/library/ms681935). 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. – Eryk Sun May 05 '15 at 14:01
  • Also, it would be simpler and more efficient to load all of your aliases in one pass using the [`/macrofile` option](https://technet.microsoft.com/en-us/library/cc753867). – Eryk Sun May 05 '15 at 14:02

1 Answers1

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.

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • 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. – Eryk Sun May 05 '15 at 15:35
  • 1
    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). – Eryk Sun May 05 '15 at 15:39
  • 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`. – Eryk Sun May 05 '15 at 15:44
  • I just tried np=notepad++.exe. Try to remove >null to see if that help – Loïc MICHEL May 05 '15 at 15:46
  • 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`](https://msdn.microsoft.com/en-us/library/ms687403) 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`. – Eryk Sun May 05 '15 at 17:50
  • When I typed `>cmd /K "doskey /macrofile=C:/temp/macros.txt & doskey /macros & newalias"` the alias is defined. `newalias=dir` but then I get the following: `'newalias' is not recognized as an internal or external command, operable program or batch file.` So it doesn't recognize the alias. It seems like it should work though, since after doing `doskey /macros` the alias is then defined. – burntoast8764 May 05 '15 at 18:06
  • @hwilli, For the first two commands, cmd finds `doskey` as `C:\Windows\System32\doskey.exe`. For the last one it tries and fails to find a file named "newalias". Aliases are substituted only when they match at the beginning of a line. It's also not enough to just be at the beginning of a line; if the command line is from a pipe or batch file it also won't work; AFAIK it has to come via conhost.exe. That's the process that manages the console window, input and screen buffers, input history (e.g. F7 popup), command-line editing, and the per-executable aliases. – Eryk Sun May 05 '15 at 18:31
  • @hwilli sorry to have confuse you . what worked for me is calling `cmd /K "doskey /macrofile=c:\temp\macros.txt & doskey /macros >null"` then I can use the newalias in the console but this `cmd /K "doskey /macrofile=c:\temp\macros.txt & doskey /macros >null & newalias` is not working as per @eryksun explanation. BTW as long as it scripted why do you need an alias ? – Loïc MICHEL May 06 '15 at 08:32
  • @Kayasax I'm using this application [Launchy](http://lifehacker.com/5963597/why-you-should-be-using-an-app-launcher-and-how-to-make-it-do-anything-you-want) and I wanted to use it's runner plugin which can pull up the command prompt and run a command that you give it quickly. It is really not that big of a deal. I was just trying to get it to use an alias that tails a commonly used log file. I was just wondering if what I was trying to do could be done. If not, it's no problem. I can just type it manually. Thanks for the help. – burntoast8764 May 06 '15 at 12:35
  • @eryksun Yes I tried with alias as second command `dir & newalias`, and it doesn't work, but `newalias & dir` works. I didn't know this before. – burntoast8764 May 06 '15 at 12:44