Tuesday, October 06, 2020

Creating Command Prompt aliases and shortcuts Using Windows DOSKEY.



The following describes the steps to run a batch script that creates aliases using DOSKEY when opening command prompt. The batch file also creates an alias command that can be used to create aliases during the command line session.

Steps

  1. Create a Batch file with the commands
  2. Create shortcut that will run cmd with the batch file as input to the /k switch

Create a Batch file with the commands

  • Open Notepad and copy the section between the markers and paste into notepad
    save the file to %USERPROFILE%\cmdrc.cmd
    ==== copy from @echo off to @echo on ====
    @echo off
    DOSKEY alias=if ".$*." == ".." ( DOSKEY /MACROS ) else ( DOSKEY $* )
    DOSKEY ls=DIR $* 
    DOSKEY cp=COPY $* 
    DOSKEY xcp=XCOPY $*
    DOSKEY mv=MOVE $* 
    DOSKEY clear=CLS
    DOSKEY h=DOSKEY /HISTORY
    DOSKEY history=DOSKEY /HISTORY
    ::compound commands
    set REPOS=%USERPROFILE%\Repos
    DOSKEY repos=cd /d %REPOS%$Tdir /a:d /b
    DOSKEY waffle=cd /d %REPOS%$Tdir /a:d /b$Tcd waffle$Tdir /a:d $Tgit branch
    DOSKEY bw=cd /d %REPOS%$Twaffle-build.cmd
    @echo on
    ==== copy from @echo off to @echo on ====
  • Customise your shortcuts
    • Place command separator as $T 
    • when you edit the cmdrc.cmd file run it again
      • %USERPROFILE%\cmdrc.cmd

Create a Shortcut to run the resource file

  • Open Windows Explorer and create a New Shortcut
  • Open the Shortcut Properties
  • Select the General Tab and Name the Shortcut e.g.
    • DosKey Alias - Command Prompt



  • Select the Shortcut Tab and set the Target
    • %windir%\system32\cmd.exe /k %USERPROFILE%\cmdrc.cmd


Tips on using the new alias batch file

Place command separator as $T in usual batch as an && or ||

This line "DOSKEY repos=cd /d %USERPROFILE%\repos$Tdir /a:d" in the batch below


In the command prompt window; the execution flow will result in the following when USERPROFILE is c:\users\myusername\repos
1. change to the device C:  
2. change to the directory to c:\users\myusername\repos
3. list directories only

Creating an alias 
while in the command prompt is opened using the Target as described above, you can create alias using the alias command.
alias lsb=DIR /b $*

Testing the new alias 
lsb c:\windows


No comments: