Home » Questions » Computers [ Ask a new question ]

Create an alias in Windows XP

Create an alias in Windows XP

Back in school, I used to have a .login file along the lines of

Asked by: Guest | Views: 36
Total answers/comments: 4
Guest [Entry]

"Not many people seem to know about it, but you can use the doskey built-in macro tool, the only issue is that it doesn't save. There are many ways to work around this though.

usage:

doskey ls=dir

ls will now do a directory listing just like dir would.

If you want to use arguments with the commands, use this syntax:

doskey d=dir $*

As for the workaround to make them save:

save all aliases to a file in this format:

doskey ls=dir
doskey ..=cd ..

and place it in one of the directories in your path. Name it something short like a.cmd, so when you open cmd you can type a to load your aliases.

If typing an a and pressing Enter seems too much work, throw this into your AutoHotkey script:

WinWaitActive, C:\WINDOWS\system32\cmd.exe
Send {a}{Enter}

Loading aliases automatically:

You can change all shortcuts to cmd to point to %SystemRoot%\system32\cmd.exe /K C:\path\to\aliases.cmd, replacing C:\path\to\aliases.cmd with the location of your aliases file. If you typically run it from the run box, you can:

Rename the cmd executable to cmd2.exe for example, and replace it with a script or another executable which launches the above command (I wouldn't really recommend this method as a lot of apps depend on cmd)
Make a batch script and call it cmda (cmd with aliases) for example. Have it launch the above command and put this batch script somewhere in your path."
Guest [Entry]

"My answer is similar to vriolk's

I created a .bat file that contained my macros (e.g. c:\winscripts\autoexec.bat):

@doskey whereis=c:\winscripts\whereis.cmd $*
@doskey ls=dir /b $*
@doskey l=dir /od/p/q/tw $*

and then from a cmd prompt ran ""cmd /?"" to find the registry key to edit for the cmd autorun:

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

using regedit, add the path for your macro batch file to the AutoRun value (add the AutoRun key if it's not there):

c:\winscripts\autoexec.bat

now whenever you run ""cmd"" from the Start->Run prompt, this autoexec.bat will also run and create the doskey macros for you.

By the way, whereis.cmd contains this:

@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT ""%%~$PATH:i""=="""" echo %%~$PATH:i

which searches your PATH variable for the term you provide:

c:>whereis javaw
c:\jdk\bin\javaw.exe"
Guest [Entry]

"The way I did it was with a quick python script:

import sys
import string
import os
import glob

def listAll():
for infile in glob.glob(""c:\\aliases\\*.bat""):
fileName = infile
fileName = fileName[len(""c:\\aliases\\""):len(fileName)-4]
fileContents = open(""c:\\aliases\\"" + fileName + "".bat"", ""r"")
fileContents.readline()
fileContentString=fileContents.readline()
fileName += "" is aliased to ""
fileName += fileContentString[0:len(fileContentString)-3]
print fileName

def listSome(which):
for infile in glob.glob(""c:\\aliases\\*.bat""):
fileName = infile
fileName = fileName[len(""c:\\aliases\\""):len(fileName)-4]
fileContents = open(""c:\\aliases\\"" + fileName + "".bat"", ""r"")
fileContents.readline()
fileContentString=fileContents.readline()
if fileName.find(which)==0:
fileName += "" is aliased to ""
fileName += fileContentString[0:len(fileContentString)-3]
print fileName

if len(sys.argv)>1:
if sys.argv[1]!=""-p"":
file = open(""c:\\aliases\\""+sys.argv[1]+"".bat"", ""w"")
file.write(""@ECHO OFF\n"")
counter=0
totalInput=""""
counter=0
for arg in sys.argv:
if counter > 1:
totalInput+= arg + "" ""
counter+=1

if totalInput.find("".exe"")!=-1:
file.write(""\"""")

counter=0

for arg in sys.argv:
if counter > 1:
file.write(arg)
if sys.argv[1]==sys.argv[2]:
if counter==2:
file.write("".exe"")
temparg=str(arg)
if temparg.find("".exe"")!=-1:
file.write(""\"""")
file.write("" "")
counter+=1
file.write(""%*"")

print ""Aliased "" + sys.argv[1] + "" to "" + totalInput
else:
if len(sys.argv)>2:
listSome(sys.argv[2])
else:
listAll()
else:
listAll()

Apologies for the poor scripting, but the usage is quite nice, imo.
Place it somewhere in your path, add .py to your PATHEXT, and add c:\aliases to your PATH too (or change it, whatever suits), then use:

alias <command> <action>

to alias (Yep, no =, though it wouldn't be hard to add a .split in there), and:

alias -p <command or part of>

To display what something is.

Hackish, but stupidly useful. There's an equivalent unalias script, but I'm sure you can work that one out.

edit: This obviously requires python, written on v26 but will probably work in anything recent. As before, sorry for the quality :)

edit2: Actually, something like this but to add to the doskey stuff would be better. You can add startup commands to cmd with the autorun registry key, too, so that could be much cleaner."
Guest [Entry]

"Batch files support a macro creation syntax. You can just declare macros as variables, and pass them arguments like functions.

Macros is a little bit hard to write, but they can be more powerful, faster, efficient than DOSKEY based macros.

Your three examples can be done with macros, but the syntax will be different in windows.

For persistent macros, you can create a MACROS.cmd file with macros, and call it in your script or add to AutoRun registry key (As @user29888 said).

How-to: Create Batch file macros

A macro allows you to embed blocks of code in a variable. Like calling a subroutine or function this allows reusing the same block of code multiple times, the difference is that by placing the code in a variable the performance will be much faster.

This is an advanced technique but can be very useful in scripts that include large blocks of code or looping commands where the performance of other methods is too slow.

A macro which runs exit /b

set _ex=exit /b

You can then exit a subroutine with:

%_ex%

A macro which lists all the .XLS files in a folder:

Set _macro=Dir c:\demo\*.xls We can now run the Macro like this: %_macro% A macro to allow comments within bracketed code blocks [source]:

Set ""[=rem/||("" & set ""]=)""

For %%G in (first second) do (
Echo first not commented line of the %%G execution
%[%
this is a multi line
comment
%]%
Echo second not commented line of the %%G execution
)

So far so like the DOSKEY command, but to make this more powerful you will want to pass arguments to the macro, in the example above you might want to pass the name of the folder to be listed.

Passing arguments to a macro is not particularly easy, the best method (discovered by Jeb) is to place a For /L command within the macro, set to run 2 steps each time the macro is run:

In the first step the arguments are stored in a variable (argv)

In the second step the main body of the macro runs and can (optionally) read the variable

The basic structure of the macro definition:

Set _macro=For /l %%n in (1 1 2) do if %%n==2 (Echo Main MACRO goes here.) else setlocal enableDelayedExpansion ^& Set argv=,

%_macro% arg1 arg2

:: The macro will expand to:
:: for /l %%n in (1 1 2) do if %%n==2 (Echo Main MACRO goes here.) else setlocal enableDelayedExpansion & Set argv=, arg1 arg2

:: which is equivalent to:
:: setlocal enableDelayedExpansion
:: Set argv= arg1 arg2
:: Echo Main MACRO goes here.

Examples:

@echo off
cls
setlocal DisableDelayedExpansion
set LF=^

::Above 2 blank lines are required - do not remove
set ^""\n=^^^%LF%%LF%^%LF%%LF%^^""

set _macrodemo=for /L %%n in (1 1 2) do if %%n==2 (%\n%
for /F ""tokens=1 delims=, "" %%G in (""!argv!"") do (%\n%
echo _argument1=%%G %\n%
set ""_argument1=!%%~G!""%\n%
dir !_argument1!\*.xls%\n%
) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,

set ""_testfolder=c:\demo""
%_macrodemo% _testfolder

In a macro where more than one argument need to be passed, these will become tokens 2,3,4... in the FOR / F command.

A library of macros can be defined by running a single MACROS.CMD batch file, allowing the library macros to be used in multiple batch files.

Source: ss64.com/nt/syntax-macros.html"