109

I used to work on bash and benefit a lot from alias. Is there any equivalent way in Windows Command Line?

I don't want to simulate a Linux environment, so cygwin is not a choice. I just need some shortcut for some very long command, like cd a_very_long_path.

3

9 Answers 9

103

As Christian.K said in his comment, the DOSKEY command can be used to define macros, which are analogous to aliases.

doskey macroName=macroDefinition

Macro parameters are referenced in the definition via $ prefixed positions: $1 through $9 and $* for all.

See the doskey technet documentation, or type doskey /? or help doskey from the command line for more information.

But there are serious limitations with DOSKEY macros:

  • The macros only work on the interactive command line - they do not work within a batch script.
  • They cannot be used on either side of a pipe: Both someMacro|findstr '^' and dir|someMacro fail.
  • They cannot be used within a FOR /F commands: for /f %A in ('someMacro') do ... fails

The limitations are so severe that I rarely use DOSKEY macros.

Obviously you can create batch scripts instead of macros, and make sure the script locations are in your PATH. But then you must prefix each script with CALL if you want to use the script within another script.

You could create simple variable "macros" for long and oft used commands, but syntax is a bit awkward to type, since you need to expand the "macro" when you want to use it.

Definition:

set "cdMe=cd a_very_long_path"

Usage (from command line or script)

%cdMe%
8
  • 1
    Thanks, then is there any way to work within a batch script?
    – StarPinkER
    Mar 4, 2013 at 12:19
  • @JermaineXu - No way with DOSKEY macros. See my updated answer for crude alternatives.
    – dbenham
    Mar 4, 2013 at 12:31
  • 2
    The pro of the second solution is that, piping will work. The first solution with doskey will not work when you want to use the macro in piping. Dec 20, 2014 at 8:39
  • ?how can I put this macros visible to the Win+R box (Run dialog)
    – ZEE
    Jan 5, 2017 at 21:21
  • @ZEE - You can't. DOSKEY macros must be physically keyed into a console window that is running cmd.exe.
    – dbenham
    Jan 5, 2017 at 21:32
28

You can make a batch script and save it into your path.

On Linux you would make a script and add it to the folder ~/bin on windows you can do the same.

Add %USERPROFILE%\bin to your PATH environment variable. Then save your scripts in there.

quickcd.cmd

@echo off
cd /d a_very_long_path

Now you can type quickcd at the command line.

It can also be called inside a script using the call function

call quickcd
4
  • This doesn't work - setting the directory within a batch file only affects the batch file. The path reverts when the batch file exits.
    – Shadow
    Mar 20, 2019 at 23:31
  • 2
    It's working for me. Microsoft Windows [Version 10.0.17134.648] Mar 23, 2019 at 9:24
  • That's really strange. So if you copy and paste the above example (changing the directory to one that exists) it works for you?
    – Shadow
    Mar 23, 2019 at 12:26
  • Is your folder on another drive? You might need to add the /d flag to CD Mar 23, 2019 at 18:29
21

subst

If you're really trying to get around something like this:

C:> cd \users\myLoginID\documents\clients\MTV\strategy\roadmap\deliverable\final

You can use the subst command to map that long path to a separate drive letter

subst m: c:\users\myLoginID\documents\clients\MTV\strategy\roadmap\deliverable\final

Then, when you want to jump into that folder, you can just type m: at the command line.

The advantage of this over doskey is that it works in all batch programs, and shows up in any file dialog box within Windows.

If you don't want the mapping any more:

subst m: /D
1
  • 5
    I also like this but it DOES eat up drive letters.
    – ggb667
    Sep 11, 2014 at 21:10
10

You could use the same trick, that windows uses: set an environment-variable (or just a variable in a batch-context) for example there is an environment-variable %windir% (and some others) So you can do an

cd C:\Windows\

or

cd %windir%

which does the same. So all, you have to do is:

set "mydir=C:\very\long\path\to\my\data\"

after that you can do (from whereever you are):

dir %mydir%

or

cd %mydir%

or whatever you want.

0
10

Unlike Linux, in Windows aliases can take arguments, and can be given parameters to assign those arguments into in their definition.

The internal built-in command is doskey, meaning it's not an external executable file located in %SystemRoot%\System32 but is a feature of cmd.exe which itself is located in %SystemRoot% or %Windir% both pointing at C:\Windows folder by default.

Doskey defines aliases among many other things. In Windows terminology, aliases is called macros. To set an alias you use

doskey alias=command $1 $2 ... $9 or $* (to catch them all) 

Here $1 points to the first argument, and $* points to the all arguments.

I'd always use $* and double quote $1 if there's only one argument to omit spaces which are treated as special characters, namely as separators (for commands).

Moreover, you can define your aliases macros in a separate file, say LinuxAliases.macro and then source it using

doskey /macrofile=LinuxAliases.macro

It will load all the aliases defined inside the file for the current session only, meaning when you exit out of cmd.exe process (also known as the shell and Command Prompt), they're gone.

To have permanent aliases you can type the full command somewhere in a regedit key called either AutoRun or AutoCommand or something similar, what I cannot recall now alongside the macro file's internal syntax :P


Here are the official, and even better documentations for the command.

5

You can use doskey.exe, here is an example:

doskey qcd=cd [pathname]

And now if you type qcd then it will cd to that path.

Here is some help with doskey macro's:

doskey [macroname]=[command]

That is the simplest form, here is an example:

doskey word=echo This is a really, really long sentence.

and if you type word the output is:

This is a really, really long sentence.

You can use $1-$9 for parameters, here is an example:

doskey npd=notepad $1

and if you type npd it will open notepad but if we type npd test.txt it will open test.txt in notepad.

You can use $* for all parameters, here is another example:

doskey prt=echo $*

and if you type prt This is short. then the output will be:

This is short.

You can use $T for multiple commands, here is an example:

doskey newline=echo $1 $T echo $2

and if you type newline Hello and then the output will be:

Hello

and

I hope you understand.

1

I've made an "alias" like that specifically for changing directory. Basically I created a file named fav.bat and put it into a folder in PATH (e.g C:\MyBatches):

@setlocal
@echo off
set userChoice=%1
rem The format is:
rem call :condition SHORTCUT FOLDER
call :condition web c:\Git\SecThor\SecWeb\App\
call :condition cloud c:\Git\SecThor\SecCloud\dojo.backend\
call :condition good c:\Users\Vitaly\Dropbox\Projects\goodread-friends\
endlocal
popd
GOTO :eof

:condition
rem We do PUSHD twice since endlocal does popd once :|
if "%userChoice%"=="%1" pushd %2 & pushd %2 & echo pushd %2
if "%userChoice%"=="" echo %1 =^> %2
GOTO :eof

Then I can jump to any predefined folder very quickly. I wrote a full article about that technique here.

1

This is an old question, but was still #2 result when googling this issue before I found a fix so I'll share my solution.

  1. Create a folder for your scripts C:\shell-scripts
  2. Add created folder to PATH
  3. Inside that folder, create a alias.cmd and/or alias.ps1 (depending on if you use CMD or PowerShell)
  4. Contents of file:
    • pushd "C:\some\path\here"
      
  5. Make sure you close and re-open your terminal to load PATH
  6. Run alias

Note on my machine I was able to make both a .cmd and a .ps1 file with the same name without conflicts so I can use the alias in either PowerShell or CMD.

0

assuming you remain on the same drive, i.e. no D: then cd D:\somedir\ needed.

In my case, C: holds system files, but all work is on D:

2 parts. first set some working directories, using a batch file.

let's say localprofile.cmd. You can have multiple ones of these, just run them as needed.

set wd1=D:\home\work\fb440.dumper
set wd2=D:\home\work\py\tests

now use another command file to move around, based on those environment variables you just set.

wd.cmd

echo off
d:
if %1.==. set | findstr wd
if %1==1 cd %wd1%
if %1==2 cd %wd2%
if %1==3 cd %wd3%
if %1==4 cd %wd4%
if %1==5 cd %wd5%
if %1==6 cd %wd6%

And a bit of a sample use:

D:\home\bin>wd 2

D:\home\bin>echo off
D:\home\work\py\tests>wd 1

D:\home\work\py\tests>echo off

wd by itself is supposed to show me the list of bookmarked directories. It works, but not sure why I am getting this "unexpected cd" message.

D:\home\work\fb440.dumper>wd

D:\home\work\fb440.dumper>echo off
wd1=D:\home\work\fb440.dumper
wd2=D:\home\work\py\tests
cd was unexpected at this time.

D:\home\work\fb440.dumper> 

Regular .cmd batches

Aside from directory navigation, you can create a foo.cmd somewhere on your PATH and it can act much as an alias. Remember, unlike Linux/OSX shells, cmd files can affect environment variables and the session, without needing to source them.

I have a cdbin.cmd to navigate me, for example:

d:
cd \home\bin

And this is a pseudo-grep grep.cmd

findstr -n %1 %2

in action (whatever sort /+2 /+10 means)

D:\home\work\fb440.dumper>grep class *.py | sort /+2 /+10

dumper2.py:18:class PrettySafeLoader(yaml.SafeLoader):            
dumper2.py:27:class Comparator(object):                           
dumper2.py:3:class module_settings:                               
linker2.py:5:class module_settings:                               
dumper2.py:65:class DataCompProcessor(object):                    
dumper2.py:69:class DataCompTextRenderer(DataCompProcessor):      
dumper2.py:76:class DataComparator(object):                       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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