Adding an alias in Windows 7 or making ls = dir in a command prompt

Hey all,

I don’t know about you but I switch between FreeBSD and Windows a lot.  So it drives me crazy when I type the command ls on windows and get the error message.

C:\Windows\system32>ls
‘ls’ is not recognized as an internal or external command,
operable program or batch file.

So I want this to go away.

I looked for the alias command in Windows and couldn’t find one.  So I made a batch file that solves this.

Windows doesn’t seem to have the equivalent of a .shrc or .cshrc or .bashrc. I couldn’t find a .profile either.  So I decided to go with the batch file route.

Creating a batch file as an alias

I created an .bat file that just forwards calls the original file and forwards all parameters passed when making the call.

Here is how it works.

Create a file called ls.bat. Add the following text.

ls.bat

@ECHO OFF
REM
REM Run a command with as many parameters as are passed.
REM This is used as a wrapper for any command.
REM It may also be used to alias a command.
REMREM Change this variable to equal the command you want to alias.
SET RealCMDPath=dir:getparams
SET cmdparams=%1
shift
:addparams
SET cmdparams=%cmdparams% %1
SHIFT
IF NOT %1.==. GOTO addparams

:runcmd
%RealCMDPath% %cmdparams%

Copy this batch file to your C:\Windows\System32 directory. Now you can type in ls on a windows box at the command prompt and it works.

How does this work to make your aliased command?

  1. Name the batch file the name of the alias.  I want to alias ls to dir, so my batch file is named ls.bat.
  2. In the batch file, set the RealCMDPath variable to the proper value, in my case it is dir.

So if you want to alias cp to copy, you do this:

  1. Copy the file and name it cp.bat.
  2. Edit the file and set this line:
    SET RealCMDPath=dir

Now you have an alias for both ls and cp.

Using different versions of msbuild.exe

You can also use this so you don’t have to add a path.

I need to use C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe but sometimes I want to use C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe. Both files are named the same. So I can easily use my alias command.

  1. Create two files in C:\Windows\System32: one named msbuild35.bat and one named msbuild40.bat.
  2. Change the line in each file to have the appropriate paths for the RealCMDPath.

Anyway, this is really a useful batch file.

4 Comments

  1. CaptWheeto says:

    You could always use Powershell. It seems the guys who designed it were influenced a lot by bash, also you can edit your own profile to include more aliases. It has ls and rm and a couple of other things that make working on Windows easier. If only I could use Linux/FreeBSD at work…

  2. Chris says:

    The alias command in Windows is called “alias”. Just type “alias” to see all the aliases that currently exist, or “alias /?” to see how to add an alias. You can also create a file containing a whole boatload of aliases, and then feed that file to the alias command, like this: “alias -f alias.txt”.

    Aliases can contain environment variables, but aren’t recursive (can’t contain other aliases), AFAIK.

    — Chris

    • rhyous says:

      Chris,

      I opened a command prompt and typed in alias.

      C:\Users\Jared>alias
      ‘alias’ is not recognized as an internal or external command,
      operable program or batch file.

      Does it exist on Windows 7 64-bit?
      Or do you have to install something first?

  3. Martim says:

    I used to use UWIN from AT&T. But I don’t installed it in Windows 7 …

    http://en.wikipedia.org/wiki/UWIN

    http://www2.research.att.com/sw/tools/uwin/

Leave a Reply