1

Coming from a linux shell, I'm used to being able to source a file into the current environment. How can I do this in the Windows cmd command prompt?

e.g. in linux I can create an alias for a long complicated command:

alias shortcut="some super long command with lots of --options and arguments etc"
alias another="some other super long command with lots of --options and arguments etc"
alias again="yet another super long command with lots of --options and arguments etc"

then I can save that to a file and just source aliases.bash and even automatically load it when starting the shell such as when used in a .bashrc or .bash_profile file.

I know I can do something similar at runtime in the cmd prompt with doskey

doskey shortcut="some super long command with lots of --options and arguments etc"

But how can I save all these doskey entries into a master file that I can load into the current environment? Even better, how can I load them automatically when starting the cmd prompt?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 1
    try `call` command – Yu Jiaao Oct 27 '17 at 02:45
  • @YuJiaao do you mean like `call filename.ext`? because that just tells me that `filename.ext is not recognized as an internal or external command, operable program or batch file.` – Jeff Puckett Oct 27 '17 at 02:49
  • @YuJiaao ah yes, it does work, but the filename must have the extension `.bat` thanks! Now, how would I automatically launch this when starting the prompt? – Jeff Puckett Oct 27 '17 at 02:52
  • The Windows console aliases set by doskey.exe will be a letdown compared to bash aliases. They work in the console host process, conhost.exe, which substitutes the alias that matches at the beginning of a line, and only at the beginning of a line. The application that's reading from the console, such as cmd.exe, has no clue the alias even exists. It just reads the line with the alias already substituted. So you can't pipe to an alias in CMD, and since they're a creature of the console, the aliases set by doskey.exe can't be used in a batch script. – Eryk Sun Oct 29 '17 at 08:43

2 Answers2

2

You can add an AutoRun entry to your registry to automatically run a .cmd file when cmd starts.

Create a file C:\Users\Jeff\autorun.cmd and add your doskey commands to it like so:

@echo off
doskey shortcut="some super long command with lots of --options and arguments etc"
doskey another="some other super long command with lots of --options and arguments etc"
doskey again="yet another super long command with lots of --options and arguments etc"

Then, edit HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor in your registry to contain something like:

AutoRun=C:\Users\Jeff\autorun.cmd

Based on information provided from https://superuser.com/a/144348/201002.

Taylor Hx
  • 2,815
  • 23
  • 36
  • You can use the `/macrofile=filename` argument to install console aliases for multiple executables in named sections such as "[cmd.exe]" and "[python.exe]". Also, it should check and set an environment variable to prevent reloading the aliases. Every time CMD executes without the /D option it executes the AutoRun key, so bear in mind that the C runtime does not use /D for the `system` command, nor do other libraries such as Python's subprocess with `shell=True`. At least CMD itself uses /D when running another instance of itself for a pipe. – Eryk Sun Oct 29 '17 at 08:34
1

Answered here: https://stackoverflow.com/a/21040825/3173271

You load up your commands into a bat, and then get them run into the command processor at logon by creating an AUTORUN entry in the registry pointing to the batch file.

LeeM
  • 1,118
  • 8
  • 18