How can I make command aliases in DOS like I would with bash?

I found out about doskey in a forum thread, so I can do something like:

doskey ls=dir /b

...and now the command ls acts a little more like ls on Unix. (I type ls so often in DOS, it isn't even funny.)

But how do I get this to stick between sessions? It goes away the next time I open cmd.exe. (Is there something like .bash_profile?)

link|improve this question

Sorry to make it offtopic, but you can benefit from installing a copy of cygwin and have all the unix tools at your palmrest – bubu Jun 8 '10 at 18:06
Yeah, I'd like to, but I don't think that's an option (and might be more work than it's worth). My primary machines are Linux and OS X -- I'm just testing some programs on Windows right now. – Benjamin Oakes Jun 8 '10 at 18:10
there isn't much thing to do. cygwin.com/setup.exe is all you need to install. just download and click next. it's all there and very simple. there isn't much to configure either. – bubu Jun 8 '10 at 18:18
Hrm... well, I'll try it out. The machine has a lot of problems already though... ;) – Benjamin Oakes Jun 8 '10 at 18:40
4  
unless your MS-DOS is version 6.22 or less, you aren't running DOS. by your mention of cmd.exe i assume you're talking about the basic WinNT/2k/XP/etc command shell. – quack quixote Jun 8 '10 at 18:56
show 2 more comments
feedback

6 Answers

up vote 0 down vote accepted

cygwin can be used in this case although this is not exactly an on-topic answer.

http://www.cygwin.com/ CygWin
Download: http://cygwin.com/setup.exe

To access it easily in windows, you can put c:\cygwin\bin in your path.

note that there are a few command that clashes with windows software which is not equivalent e.g. find(1) vs find.exe -- find(1) lists all files and subdirectories whereas find.exe functions like grep.

another option is to access

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

check out

 cmd.exe /? 
for more details

link|improve this answer
Hrm... it doesn't seem to use any of my Windows path... I'll have to play around with this later, I think. (Never used cygwin seriously before.) Thanks for your help so far. – Benjamin Oakes Jun 8 '10 at 19:10
adding the cygwin path into the windows path environment variable allows you to use the tools in the cygwin environment in cmd.exe; just note that there ARE caveats e.g. find.exe clashes with find command provided by cygwin. – bubu Jun 8 '10 at 19:15
Why the undervote? I just have no idea. – bubu May 31 '11 at 19:01
feedback

It is rather easy setup permanent alias in command prompt for Windows using the @DOSKEY command and HKCU\Software\Microsoft\Command Processor Autorun option.

Quick step-by-step guide:

  1. Create a new batch file, call it Alias.bat. Copy/paste the text below. TIP: I recommend creating a C:\Bin folder for all your command line tools.
  2. Open the register HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
  3. Add an String Value named Autorun and set the value to absolute path of the Alias.bat file.
  4. Done.

This batch file will execute every time you open a command prompt.

Contents of Alias.bat

@ECHO OFF 
@DOSKEY ls=DIR $* 
@DOSKEY cp=COPY $* 
@DOSKEY xcp=XCOPY $*
@DOSKEY mv=MOVE $* 
@DOSKEY clear=CLS
@DOSKEY h=DOSKEY /HISTORY

HTH,

link|improve this answer
1  
Simple, easy to update once setup, I like this one a lot! – Urda May 31 '11 at 14:22
Unfortunately I have since stopped using these alias, as the @DOSKEY command causes a crash when exiting Autodesk Maya 2010. There is probably a work around, e.g. checking if parent process is Maya, however I have not spent time investigating. – Dennis Jun 3 '11 at 13:20
feedback

There is a registry entry at HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun which allows you to run a command when you start a cmd prompt. This includes a batch file.

link|improve this answer
You may want to point to HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun as others will not have the same user ID as you. – bubu Jun 8 '10 at 19:17
@bubu: Oh, very true, sorry. I found that by searching my registry, didn't think. – Phoshi Jun 8 '10 at 19:21
feedback

Also sort of off-topic - Use PowerShell instead of the DOS command line. The good news is that PowerShell has the equivalent of .bash_profile, and runs just like the DOS command line. It comes with a built-in alias generation feature. The bad news is that there is a bit of a learning curve if you want to do anything more complicated than simple DOS commands.

By the way, 'ls' is defined as an alias of 'dir', right out of the box.

link|improve this answer
feedback

Try putting them in autoexec.bat. I'm not sure, you'll have to try it. Put autoexec.bat itself in C:\

link|improve this answer
Doesn't seem to work... – Benjamin Oakes Jun 8 '10 at 18:19
feedback

Put your aliases into a file called, perhaps, alias.cmd and add /K \path\to\alias.cmd to the shortcut that you use to start cmd.exe.

Reference

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.