Doskey Macros

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Doskey Macros

#1 Post by logical_bit » 13 Dec 2016 13:35

Hello, I'm a first time poster here, and I'm very happy to have discovered this website!

I was curious if it was possible to group compound statements in a Doskey macro.
Meaning would it be possible to group for loops and/or if statements or even nested if statements in a Doskey macro?

for example:

Code: Select all

@echo off
setlocal enabledelayedexpansion

doskey 1= for /L %%a in ( 1, 1, 5 ) do (
   set /a limit=5
   echo %%a
      if %%a==!limit! (
      echo loop complete^^!
      )
)

REM This way all I would need to do to output the results would be:
1 >> %UserProfile%\Desktop\Loop_Results.txt

endlocal

How would I incorporate a doskey macro that has several compound statements within it to redirect or append to a text file? Is this possible? Is there a simple way of achieving this that I'm overlooking? Ugly hacks will do :)

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#2 Post by Squashman » 13 Dec 2016 14:19

Code: Select all

H:\>doskey test=set limit=5 $T FOR /L %a in (1,1,5) do @if %a==%limit% echo loop complete

H:\>test

H:\>loop complete

H:\>

logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Re: Doskey Macros

#3 Post by logical_bit » 13 Dec 2016 14:31

Squashman wrote:

Code: Select all

H:\>doskey test=set limit=5 $T FOR /L %a in (1,1,5) do @if %a==%limit% echo loop complete

H:\>test

H:\>loop complete

H:\>


Thank you! Always a pleasure to learn something new :)

edit:
I tried this:

Code: Select all

D:\>doskey a=set limit=5 $T for /l %a in (1,1,5) do @echo %a $T @if %a==%limit%
echo loop complete
D:\>a >> loop_result.txt

D:\>1
2
3
4
5

D:\>
D:\>
D:\>dir  | findstr loop_result.txt

D:\>


Appending doskey macro does not work... any idea how to do this?

Thanks for your help
Last edited by logical_bit on 13 Dec 2016 14:58, edited 2 times in total.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#4 Post by Squashman » 13 Dec 2016 14:42

Code: Select all

H:\>doskey test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))^>log.txt

H:\>doskey /macros
test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))>log.txt

H:\>test

H:\>type log.txt
1
2
3
4
loop complete

H:\>

logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Re: Doskey Macros

#5 Post by logical_bit » 13 Dec 2016 14:59

Squashman wrote:

Code: Select all

H:\>doskey test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))^>log.txt

H:\>doskey /macros
test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))>log.txt

H:\>test

H:\>type log.txt
1
2
3
4
loop complete

H:\>


I appreciate your help. I keep getting "a was unexpected at this time."

I copied your code exactly. Any idea on what's happening?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#6 Post by Squashman » 13 Dec 2016 16:30

Do exactly what I did and then copy and paste everything into the forums .

logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Re: Doskey Macros

#7 Post by logical_bit » 14 Dec 2016 15:46

Squashman wrote:Do exactly what I did and then copy and paste everything into the forums .

Code: Select all

D:\>type "doskey loop.cmd"

@echo off

rem doskey for loop

doskey test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop c
omplete) else (echo %a))>%UserProfile%\Desktop\log.txt
echo.
exit /b


D:\>doskey /macros
test=set limit=5 $T (FOR /L a== (echo loop complete) else (echo UserProfile\Desk
top\log.txt

D:\>test

D:\>a was unexpected at this time.

D:\>


When we examine the doskey /macros command, there are parts of the .cmd script missing in "test". Thanks for looking into this.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#8 Post by Squashman » 14 Dec 2016 17:18

And what is the difference between how I did it and how you are trying to do it?

logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Re: Doskey Macros

#9 Post by logical_bit » 14 Dec 2016 17:24

Squashman wrote:And what is the difference between how I did it and how you are trying to do it?


None. As you can see I literally copied and pasted your code.

I've no idea why the prompt is spitting out "a was unexpected at this time"...

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#10 Post by Squashman » 14 Dec 2016 17:31

Really? How am I executing my code versus yours?

logical_bit
Posts: 6
Joined: 13 Dec 2016 13:10

Re: Doskey Macros

#11 Post by logical_bit » 14 Dec 2016 18:36

Squashman wrote:Really? How am I executing my code versus yours?


I noticed this in the doskey help file

Code: Select all

D:\>doskey /?
Edits command lines, recalls Windows commands, and creates macros.

DOSKEY [/REINSTALL] [/LISTSIZE=size] [/MACROS[:ALL | :exename]]
  [/HISTORY] [/INSERT | /OVERSTRIKE] [/EXENAME=exename] [/MACROFILE=filename]
  [macroname=[text]]

  /REINSTALL          Installs a new copy of Doskey.
  /LISTSIZE=size      Sets size of command history buffer.
  /MACROS             Displays all Doskey macros.
  /MACROS:ALL         Displays all Doskey macros for all executables which have
                      Doskey macros.
  /MACROS:exename     Displays all Doskey macros for the given executable.
  /HISTORY            Displays all commands stored in memory.
  /INSERT             Specifies that new text you type is inserted in old text.
  /OVERSTRIKE         Specifies that new text overwrites old text.
  /EXENAME=exename    Specifies the executable.
  /MACROFILE=filename Specifies a file of macros to install.
  macroname           Specifies a name for a macro you create.
  text                Specifies commands you want to record.

UP and DOWN ARROWS recall commands; ESC clears command line; F7 displays
command history; ALT+F7 clears command history; F8 searches command
history; F9 selects a command by number; ALT+F10 clears macro definitions.

The following are some special codes in Doskey macro definitions:
$T     Command separator.  Allows multiple commands in a macro.
$1-$9  Batch parameters.  Equivalent to %1-%9 in batch programs.
$*     Symbol replaced by everything following macro name on command line.

D:\>


Do you think the batch parameter "$1-$9" could have anything to do with it? I am invoking a script whereas you are just typing out a line. Perhaps there is a difference?

Even when I try your code without incorporating it into a script, now I get "%a was unexpected at this time." instead of just "a was unexpected at this time." (no %)

Code: Select all

D:\>doskey test=set limit=5 $T (FOR /L a% in (1,1,5) do @if %a==%limit% (echo lo
op complete) else (echo %a))>%UserProfile%\Desktop\log.txt

D:\>test

D:\>a% was unexpected at this time.

C:\>

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#12 Post by Squashman » 14 Dec 2016 21:29

Well lets look at my code and your code top to bottom and see if we can see any errors on your part.

Code: Select all

doskey test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))^>log.txt
doskey test=set limit=5 $T (FOR /L a% in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))>%UserProfile%\Desktop\log.txt

I see two.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Doskey Macros

#13 Post by Squashman » 14 Dec 2016 22:32

So essentially what you are trying to do is load your doskey macros from a batch file. But to do what you want to do you have to understand that variable expansion changes a bit when you are running from a batch file.

From the command prompt if you echo an undefined variable it will literally echo the actual text including the percent symbols.
In a batch file the variable will expand to nothing. So basically you need to escape the variables from expanding by doubling the percent symbols. And in theory you really don't want your environmental variables to expand at this point either so you double the percent symbols on those as well.

Code: Select all

@echo off
doskey test=set limit=5 $T (FOR /L %%a in (1,1,5) do @if %%a==%%limit%% (echo loop complete) else (echo %%a))^>%%UserProfile%%\Desktop\log.txt

So here is running the batch file, checking the macro definition and then running the doskey macro.

Code: Select all

C:\Batch\doskey>load_doskey.bat

C:\Batch\doskey>doskey /macros
test=set limit=5 $T (FOR /L %a in (1,1,5) do @if %a==%limit% (echo loop complete) else (echo %a))>%UserProfile%\Desktop\log.txt

C:\Batch\doskey>test

C:\Batch\doskey>
C:\Batch\doskey>type %UserProfile%\Desktop\log.txt
1
2
3
4
loop complete

C:\Batch\doskey>

Post Reply