DOSKEY

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

DOSKEY

#1 Post by Cleptography » 08 Jun 2011 14:28

So, how can we use DOSKEY in a script?
Is this possible?

Doskey macros are a wonderful thing, even today with the GUI goodness Windows offers, there is still many a reason to drop down to the command line. Now, most Linux and UNIX folks will laugh at the arguably abysmal tool set given by the good old DOS prompt, but it can be made just a little more interesting if you start using doskey in conjunction with a set of batch files.

Below is a very simple template doskey macro/batch file which will first execute as a batch file, and then use itself as a doskey macro file. Hopefully it will get you started down a more efficient command line route. Or you might just find it a clever, but useless mess.

Code: Select all

;= @echo off
;= rem Call DOSKEY and use this file as the macrofile
;= %SystemRoot%\system32\doskey /listsize=1000 /macrofile=%0%
;= rem In batch mode, jump to the end of the file
;= goto end
;= rem ******************************************************************
;= rem *   Filename: aliases.bat
;= rem *    Version: 1.0
;= rem *     Author: Ben Burnett <me@cs.wisc.edu>
;= rem *    Purpose: Simple, but useful aliases; this can be done by
;= rem *             other means--of course--but this is dead simple and
;= rem *             works on EVERY Windows machine on the planet.
;= rem *    History:
;= rem * 22/01/2002: File Created (Syncrude Canada).
;= rem * 01/05/2007: Updated author's address, added new macros, a
;= rem *             history and some new helpful comments.
;= rem * 19/06/2007: Added Notepad, Explorer and Emacs macros.
;= rem * 20/06/2007: Fixed doskey macrofile= path problem: it is now not
;= rem *             a relative path, so it can be called from anywhere.
;= rem ******************************************************************

;= Doskey aliases
h=doskey /history

;= File listing enhancements
ls=dir /x $*
l=dir /x $*
ll=dir /w $*
la=dir /x /a $*

;= Directory navigation
up=cd ..
pd=pushd

;= Copy and move macros
cp=copy
mv=move

;= Delete macros
rm=del /p $*
rmf=del /q $*
rmtmp=del /q *~ *# 2>nul

;= Fast access to Notepad
n=notepad $*

;= Fast access to Explorer
c=explorer .

;= :end
;= rem ******************************************************************
;= rem * EOF - Don't remove the following line.  It clears out the ';'
;= rem * macro. Were using it because there is no support for comments
;= rem * in a DOSKEY macro file.
;= rem ******************************************************************
;=


It abuses the fact that the command prompt in Windows will silently eat the ';=' prefix, while doskey will treat it as a macro being re-defined over and over again. http://ben.versionzero.org/wiki/Doskey_Macros

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: DOSKEY

#2 Post by Ed Dyreen » 08 Jun 2011 18:37

I use variable names that are easy to remember. :(

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: DOSKEY

#3 Post by dbenham » 08 Jun 2011 19:32

Cleptography wrote:So, how can we use DOSKEY in a script?
Is this possible?

No. DOSKEY Macros Must Be Executed from the Command Prompt

Dave Benham

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: DOSKEY

#4 Post by Cleptography » 08 Jun 2011 19:43

dbenham wrote:
Cleptography wrote:So, how can we use DOSKEY in a script?
Is this possible?

No. DOSKEY Macros Must Be Executed from the Command Prompt

Dave Benham

So they need to be executed from the command line but they can be set from a batch script is this what you are telling me?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: DOSKEY

#5 Post by dbenham » 08 Jun 2011 19:47

That is what the documentation says!

Dave

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: DOSKEY

#6 Post by Cleptography » 08 Jun 2011 20:03

...and do you know of any work arounds that would allow a DOSKEY macro to be executed from a batch script maybe something having to do with the history or any ideas at all?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: DOSKEY

#7 Post by Ed Dyreen » 08 Jun 2011 20:11

I think your on a dead end.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: DOSKEY

#8 Post by Cleptography » 08 Jun 2011 20:23

Dead end I am just asking questions is all, isn't this what this forum is for asking questions?
"you're" "your" oh who noes anymore. :roll:
It is fun to take exhaustive testing and miles of code and turn it into amusing experiments.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: DOSKEY

#9 Post by Ed Dyreen » 08 Jun 2011 20:27

I've seen some cool things with doskey though, but I forgot :(

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: DOSKEY

#10 Post by Cleptography » 02 Jul 2011 08:48

This was an accident while trying to create a dummy profile for the autocom project and I do not understand what is going on here or how this works, and correct me if I am wrong but are these macros not being executed from within the batch script.... :?

BATCH:

Code: Select all

@echo off
@Title Autocom Command Line

REM Create the header
@echo;Autocom Command Line [Version %ver#%]
@echo;Copyright (c) 2011 Autocom all rights reserved
@echo;

:initialize
REM Initialize the prompt
@set /p DoCommand=Autocom\^>
%DoCommand%
goto :initialize


So once this is started and I type in something like

Code: Select all

Autocom\>doskey HELLO=echo;WHAT

And then...

Code: Select all

Autocom\>HELLO

I get...

Code: Select all

WHAT

How is this working?
If I execute...

Code: Select all

Autocom\>prompt C:\^>

Nothing works until I

Code: Select all

Autocom\>cmd

Then the the prompt is shown.
Can someone please explain this behavior to me.
Thank you

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: DOSKEY

#11 Post by trebor68 » 05 Jul 2011 04:15

The macro that is definated with DOSKEY is only a command (or more).

But here is the diffence between the macros and batch files.

All macros can you definated in only one batch file.
The same commands are can you also definated in different batch files with the name of the macro.

You can display/save the commands that you have input.

Code: Select all

DOSKEY /HISTORY

It is also possibel to overwrite a command.
Here a Example for the internal command CD:
When you will change the directory (folder) and the drive then must you select the parameter "/d". But most you will forget this. With this macro is that not a problem.

Code: Select all

DOSKEY CD=CD /d $*

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: DOSKEY

#12 Post by dbenham » 05 Jul 2011 09:10

Clepto - I can never tell when you are toying with us vs. when you have an honest question to which you don't already know the answer :wink:

I'll bite.

I modified your batch file slightly and I think it helps answer both your questions.

Code: Select all

@echo off
Title Autocom Command Line
REM Create the header
echo;Autocom Command Line [Version %ver#%]
echo;Copyright (c) 2011 Autocom all rights reserved
echo;

echo on
:initialize
@REM Initialize the prompt
@set /p DoCommand=Autocom\^>
@echo(
@set DoCommand
%DoCommand%
@echo(
@goto :initialize

Question 1 explanation
Before running the modified batch I define the following DOSKEY macro.

Code: Select all

doskey doublet=echo part1$Techo part2

And here is an interactive session after starting the modified batch file:

Code: Select all

Autocom Command Line [Version ]
Copyright (c) 2011 Autocom all rights reserved

Autocom\>doublet

DoCommand=echo part1

D:\utils>echo part1
part1

Autocom\>
DoCommand=echo part2

D:\utils>echo part2
part2

Autocom\>
The SET /P command must read directly from the keyboard instead of reading stdin, just as the DOSKEY macros do. So DOSKEY intercepts the doublet text and expands it before SET /P gets a chance to read it. Now the full definition of doublet is in the keyboard buffer and SET /P reads the first line, it is displayed and executed. Then the batch file loops back where SET /P reads the 2nd line of the buffer and it is displayed and executed.

I used to wonder why the following doesn't work:

Code: Select all

echo value | set /p var=prompt
This puzzle helped me discover that SET /P doesn't read stdin.

I don't see how this helps with the goal of using DOSKEY macros in batch files because it requires user input at run time to initiate the DOSKEY macro processing. If there was a native way to purge the keyboard buffer and then stuff the buffer with macro commands I see how this could be helpful. I suppose AutoIT or AutoCom Send might help, but this is not native. If you are going down that road it seems like there would be a more efficient non-native solution that does the job of the macro directly.

Question 2 explanation
Your original batch never gives the prompt a chance because you have ECHO OFF :!: The Autocom\> prompt is the output of the SET /P command, not a normal prompt. When you issue the CMD command, your batch file is hibernating and the command shell is now displaying the modified prompt as expected. Issuing EXIT brings us back to your little batch file. But I'm pretty sure you already knew all of this.

Continuing the interactive session of the modified batch file:

Code: Select all

Autocom\>prompt test$G

DoCommand=prompt test$G

D:\utils>prompt test$G

Autocom\>echo hello world

DoCommand=echo hello world

test>echo hello world
hello world

Autocom\>
The test> prompt is displayed when the echo command is echoed as expected.

Dave Benham

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: DOSKEY

#13 Post by Cleptography » 05 Jul 2011 13:56

@Dave
No it was an honest question that I did not know, but you explained it to me, and makes sense now. I thank you for that. I will not try to mock anyone on this forum anymore. Ed has informed me that you and a couple others have a problem with me, so I will not behave childishly anymore.

Regards,
Mr. Blonde-

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: DOSKEY

#14 Post by trebor68 » 05 Jul 2011 14:49

@ Cleptography

The awnser to your question is following code:
If you have saved the code from your question in the file "TEXT.TXT".

Code: Select all

DOSKEY /MACROFILE=TEST.TXT


The same file as a pure batch file.

Code: Select all

@echo off
rem I changed the file so that it is a pure batch file.
:: goto end
rem ******************************************************************
rem *   Filename: aliases.bat
rem *    Version: 1.0
rem *     Author: Ben Burnett <me@cs.wisc.edu>
rem *    Purpose: Simple, but useful aliases; this can be done by
rem *             other means--of course--but this is dead simple and
rem *             works on EVERY Windows machine on the planet.
rem *    History:
rem * 22/01/2002: File Created (Syncrude Canada).
rem * 01/05/2007: Updated author's address, added new macros, a
rem *             history and some new helpful comments.
rem * 19/06/2007: Added Notepad, Explorer and Emacs macros.
rem * 20/06/2007: Fixed doskey macrofile= path problem: it is now not
rem *             a relative path, so it can be called from anywhere.
rem ******************************************************************

rem Doskey aliases
doskey h=doskey /history

rem File listing enhancements
doskey ls=dir /x $*
doskey l=dir /x $*
doskey ll=dir /w $*
doskey la=dir /x /a $*

rem Directory navigation
doskey up=cd ..
doskey pd=pushd

rem Copy and move macros
doskey cp=copy
doskey mv=move

rem Delete macros
doskey rm=del /p $*
doskey rmf=del /q $*
doskey rmtmp=del /q *~ *doskey  2>nul

rem Fast access to Notepad
doskey n=notepad $*

rem Fast access to Explorer
doskey c=explorer .

:end

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: DOSKEY

#15 Post by dbenham » 05 Jul 2011 18:11

Cleptography wrote:@Dave
No it was an honest question that I did not know, but you explained it to me, and makes sense now. I thank you for that. I will not try to mock anyone on this forum anymore. Ed has informed me that you and a couple others have a problem with me, so I will not behave childishly anymore.

Regards,
Mr. Blonde-
Glad I could help :)

But... Mr. Blonde :?: :?
I assume there is a cultural reference there that I am totally missing.
Never mind - the sadistic Mr. Blonde from the Tarantino film "Reservoir Dogs" - I knew I couldn't trust you!

@trebor68
I think Cleptography knows how to use DOSKEY. The main thrust of this thread is how to use DOSKEY macros in a batch file (or Not!). Defining the macro in a batch isn't the issue. The problem is how to use a DOSKEY macro in a batch file after it has been defined. Documentation indicates this cannot be done. Cleptography is exploring options to get around the limitation. I wish him luck in finding a useful solution :!:

Dave Benham

Post Reply