How to execute DOSKEY macros from within a batch file

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:

How to execute DOSKEY macros from within a batch file

#1 Post by Cleptography » 08 Jun 2011 20:25

POSTED BELOW:
Last edited by Cleptography on 09 Jun 2011 19:09, edited 1 time in total.

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

Re: How to execute DOSKEY macros from within a batch file

#2 Post by Ed Dyreen » 08 Jun 2011 20:29


How on earth are you going to do that, pm me if you succeed :wink:

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

Re: How to execute DOSKEY macros from within a batch file

#3 Post by Cleptography » 08 Jun 2011 20:45

POSTED BELOW:
Last edited by Cleptography on 09 Jun 2011 19:07, edited 1 time in total.

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

Re: How to execute DOSKEY macros from within a batch file

#4 Post by Cleptography » 09 Jun 2011 18:46

Here is some inspiration to get you started. The trick is to send the Macroname through a pipe into a secondary command shell. In order to close the pipe correctly CMD.EXE /K switch and a trailing "$Texit" in the macro definition are used.

Code: Select all

Doskey Macroname=Command Line$Texit
Echo Macroname | %COMSPEC% /K > NUL
Doskey Macroname=


Code: Select all

@echo Off
    If %OS%'==Windows_NT' (Echo Non-%OS% Batch File.&Goto End)
    If Not %1'==/?' Goto Begin
    Echo Sets COMPUTERNAME variable to the computername.
    Echo.
    Echo [Call] COMPVAR
    Goto End
   :Begin
    Set COMPUTERNAME=
    Doskey Computer=Echo $*$B%COMSPEC% /K > NUL
    Doskey name=Echo Set COMPUTERNAME=$*$G%TEMP%.\$OMPVAR0.BAT$TExit$TExit
    Net Config | Find "Computer name" | %COMSPEC% /K > NUL
    Call %TEMP%.\$OMPVAR0.BAT
    For %%\ In (%%) Do Echo Set COMPUTERNAME=%%%COMPUTERNAME%>%TEMP%.\$OMPVAR0.BAT
    Call %TEMP%.\$OMPVAR0.BAT
    For %%\ In (%%) Do Echo Set COMPUTERNAME=%%%COMPUTERNAME%>%TEMP%.\$OMPVAR0.BAT
    For %%C In (Call Del) Do %%C %TEMP%.\$OMPVAR0.BAT
    For %%M In (Computer name) Do Doskey %%M=
   :End


Code: Select all

@echo Off
    If %OS%'==Windows_NT' (Echo Non-%OS% Batch File.&Goto End)
    If Not %1'==/?' Goto Begin
    Echo Sets USERNAME variable to the username.
    Echo.
    Echo [Call] USERVAR
    Goto End
   :Begin
    Set USERNAME=
    Doskey User=Echo $*$B%COMSPEC% /K > NUL
    Doskey name=Echo Set USERNAME=$*$G%TEMP%.\$SERVAR0.BAT$TExit$TExit
    Net Config | Find "User name" | %COMSPEC% /K > NUL
    For %%C In (Call Del) Do %%C %TEMP%.\$SERVAR0.BAT
    For %%M In (User name) Do Doskey %%M=
    If %USERNAME%'==' Echo No user logged on.
   :End


:mrgreen:

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

Re: How to execute DOSKEY macros from within a batch file

#5 Post by dbenham » 09 Jun 2011 22:12

Doesn't work for me, and I don't see how it could work, based on what I have read.

Clepto - have you really tested this and gotten it to work, or are you simply yanking our chain? :evil:

When I invoke CMD after defining the macro, I confirmed that the CMD knows the definition of the macro, but when I pipe the macro name into CMD it claims it doesn't know the command. This is exactly the same behavior I get if I try to execute a DOSKEY macro from a batch file.

I don't know much about the low level implementation of keyboard, stdin, stdout, pipes, interrupts, etc. But I did some research via Google. Based on my limited understanding of what I have read, DOSKEY operates by reading the keyboard directly through interrupt 16. Pipes work by redirecting output of one program to stdin of the next which is read via interrupt 21. Seems like a dead end to me.

Even if it did work, it seems awfully inconvenient and slow. 1st there is the overhead of executing CMD, then transferring changed environment values via temporary batch files. Yuk. Not for me.

Dave Benham

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

Re: How to execute DOSKEY macros from within a batch file

#6 Post by Cleptography » 09 Jun 2011 23:48

It didn't work for you :?: :shock:
That's odd works for me...
I wonder what that little bit does :?: :lol:
:twisted: Anyone else not working :?:

Code: Select all

; nasm -f bin -o cmd.com cmd.asm

org 100h  ; inform Nasm that dos will load us at 100h

; get into graphics mode
    mov ax, 13h  ; ah=0, change mode - al=mode
    int 10h

; set a segreg to video memory
    push 0A000h
    pop es

    mov di, 320 * 10  ; row 10
    mov cx, 256  ; 256 colors
    mov al, 1  ; start with blue... (0 is black)

.top:
    mov [es:di], al  ; store color in memory location
    inc di  ; next memory location
    inc al  ; next color
    loop .top  ; until done

; pause to admire our masterpiece
    mov ah, 0
    int 16h

; back to text mode
    mov ax, 3
    int 10h

; back to dos
    ret

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

Re: How to execute DOSKEY macros from within a batch file

#7 Post by Ed Dyreen » 10 Jun 2011 03:11


Ok cleo, i've bookmarked this topic, I gonna look into this at a later point in time.

If it works, then that would be great, but like benham pointed out, you are causing a large overhead. Dunno, getting back to this..

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

Re: How to execute DOSKEY macros from within a batch file

#8 Post by Cleptography » 10 Jun 2011 04:00

Yes this does work, there are a few things missing but it is meant to be a starting point.
As most of my post are this one is no different it is misleading. I don't like to spell things
out.

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to execute DOSKEY macros from within a batch file

#9 Post by jeb » 10 Jun 2011 04:47

Cleptography wrote:It didn't work for you :?: :shock:
That's odd works for me...
I wonder what that little bit does :?: :lol:
:twisted: Anyone else not working :?:

On my systems it fails, too :(

I tested with

Code: Select all

( echo doskey xx=echo YY& echo doskey /macros & echo xx & echo doskey /macros & echo exit) | (%COMSPEC% echo hello )


I expect it should work ...

jeb

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

Re: How to execute DOSKEY macros from within a batch file

#10 Post by Cleptography » 10 Jun 2011 05:02

Ed Dyreen wrote:
Ok cleo, i've bookmarked this topic, I gonna look into this at a later point in time.

If it works, then that would be great, but like benham pointed out, you are causing a large overhead. Dunno, getting back to this..


Forget about the overhead and just make the damn code do what it is suppose to.
If you change a few things around does it work using command.com?
:shock:

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

Re: How to execute DOSKEY macros from within a batch file

#11 Post by Ed Dyreen » 23 May 2015 04:25

ok, I no longer use dosKey macro's and I am about to remove them from my library permanently.

I am posting the code here because I thought maybe it would be useful for someone else who for some reason would like to use doskey to store and retrieve variables or execute macro's.

-Doskey variables are unaffected by setlocal or endlocal, this allows global variables thus a way to return any variable over endlocal with little effort.
-Doskey allows 'protected' variables for simulating object attributes that should only be changed using the object's set method. Doskey memory can be used to store these protected attributes as variables.

I am posting the pseudocode because I am too lazy to convert it, but also it would obfuscate the key parts of the code.

-char 34 has to be encoded for doskey to succesfully store the data.
-the data is saved into 4 different placeholders to prevent overflow caused by encoding

That's all there is too it, it's really simple

These are the macro's

Code: Select all

:: --------------------------------------------------------------------------------------------------------------------------
( %macroStart_% runDosKeyMacro_, enableDelayedExpansion )
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2014^03^14
:: support            : naDelayed
::
:: <<implements>> setDosKeyMacro_
::
set ^"$usage.%$macro%=^
%=   =% use: ( %%runDosKeyMacro_%% #macro, #args,# )%$n1c%
%=   =% err: 0 for succes, 1 for error"
:: (
%=   =%for %%# in ("") do set ^"%$macro%=!forQ_! (1,2) do if %%?==2 (%$c1%
%$c1%
%=      =%set _="^!$function.fullPathFile^!" !$macro! $macro$ ^^^!*:~1^^^!^&if ^^^!?^^^!.==. !reDelayLH_!_%$n1c%
%=      =%set _=^^^!_:%%=%%%%^^^!%=                  delay percent for call   =%%$n1c%
%$c1%
%=   =%!forLineR_! ("^!_^!") do endlocal^&call %%r)else set ?=^^^!^&setlocal enableDelayedExpansion^&set *=%e15%!"
:: )
( %macroEnd% )
::
%@bTryAssert%
:: (
   setlocal enableDelayedExpansion &( %debugOn_% §runDosKeyMacro_ )
   :: (
      ( %setDosKeyMacro_% testMacro_ ) 2>nul
      ( %testMacro_% This, works )
   :: )
   endlocal
:: )
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
::DEPRICIATED use macroFileDump_
goto :skip "()"
:: --------------------------------------------------------------------------------------------------------------------------
( %macroStart_% getDosKeyMacro_, enableDelayedExpansion )
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2014^02^18
:: support            : naDelayed
::
:: <<implements>> setDosKeyMacro_
::
set ^"$usage.%$macro%=^
%=   =% use: ( %%getDosKeyMacro_%% #macro, #altName,# )%$n1c%
%=   =% err: 0 for succes, 1 for error"
:: (
%=   =%for %%# in ("") do set ^"%$macro%=!forQ_! (1,2) do if %%?==2 (%$c1%
%$c1%
%=      =%set _="^!$function.fullPathFile^!" !$macro!,^^^!*:~1^^^!^&if ^^^!?^^^!.==. !reDelayLH_!_%$n1c%
%$c1%
%=   =%!forLineR_! ("^!_^!") do endlocal^&call %%r)else set ?=^^^!^&setlocal enableDelayedExpansion^&set *=%e15%!"
:: )
( %macroEnd% )
::
%@bTryAssert%
:: (
   setlocal enableDelayedExpansion
   :: (
      ( %setDosKeyMacro_% testMacro_ )          ||( %assertFail_% )
      ( %getDosKeyMacro_% testMacro_ )          ||( %assertFail_% )
      ( %testMacro_% this, works, "Thi^^ s^! work^^ s^!" )    ||( %assertFail_% testMacro_ )
   :: )
   endlocal
:: )
:: --------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%endoftest%
:skip ()
::DEPRICIATED use macroFileDump_
goto :skip "()"
:: --------------------------------------------------------------------------------------------------------------------------
( %macroStart_% setDosKeyMacro_, enableDelayedExpansion )
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2015^01^28
:: support            : naDelayed
::
:: <<implements>> runDosKeyMacro_
::
set ^"$usage.%$macro%=^
%=   =% use: ( %%setDosKeyMacro_%% #macroName )%$n1c%
%=   =% err: 0 for succes, 1 for error"
:: (
%=   =%for %%# in ("") do set ^"%$macro%=!vsDelim_!(!setLocal_!!$macro!%$n1c%
%=      =%!paramL_!'$vr,"%%a"','runDosKeyMacro_,runDosKeyMacro_'%$n1c%
%=      =%!forQ_! (^^^!$vr^^^!) do set $vl=^&set $vl=^^^!%%?^^^!%$n1c%
%=      =%!necho_! ^^^!$trace^^^!%e7%(^^^!$vr^^^!%e7%)%$n1c%
%$c1%
%=      =%!for@_! ("$oldlen,^!$vr^!","$newlen,runDosKeyMacro_") do (!callMacro_!stringLen_,%%~@)2^>^&1^>nul%$n1c%
%=      =%!necho_!  oldlen : '^^^!$oldlen^^^!' bytes%$n1c%
%=      =%!necho_!  newlen : '^^^!$newlen^^^!' bytes%$n1c%
%=      =%(!necho_!  olddata: '^^^!$vl^^^!')^>^&2%$n1c%
%=      =%if defined $vl (%$c1%
%=         =%set $0=^^^!$vl:~0,2048^^^!%$n1c%
%=         =%set $1=^^^!$vl:~2048,2048^^^!%$n1c%
%=         =%set $2=^^^!$vl:~4096,2048^^^!%$n1c%
%=         =%set $3=^^^!$vl:~6144^^^!%$n1c%
%=         =%!forÇ_! (0,1,3) do (%$c1%
%=            =%!advEncoLH_!$%%ç,/KCF%=               coded mark set   =%%$n1c%
%=            =%set $%%ç=^^^!$%%ç:^^^"=§q^^^!%=            enco     34   =%%$n1c%
%=            =%doskey.EXE /exename=^^^!$vr^^^!.%%ç _=_^^^!$%%ç^^^!%$c1%
%=         =%)%$n1c%
%=         =%(!necho_!  newdata: '^^^!runDosKeyMacro_^^^!')^>^&2%$n1c%
%=         =%set %%~a=^^^!runDosKeyMacro_:$macro$=%%~a^^^!%$c1%
%=      =%)else !exception.set_!1%$n1c%
%$c1%
%=   =%!forQ_! (^^^!£e^^^!) do (%$c1%
%=      =%!exception.nPrintL!^&!endlocalL_!^^^!$r^^^!,%%a%$n1c%
%=      =%!setExitCode!%$c1%
%=   =%))else setlocal enableDelayedExpansion^&set *="
:: )
( %macroEnd% )

And these are the functions

Code: Select all

:: --------------------------------------------------------------------------------------------------------------------------
:§getDosKeyMacro_ ( #$macro, #$altName,# )
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2014^06^29
:: support            : naDelayed
:: languages          : N/A
::
rem ( %debugOn_% "!$trace!" ) &( %debug_% "", "inside !$trace!(!§p!)", #equ )
::
( %forP_% '$macro,"%%a"', '$altName,"%%b","%%a"' )
:: (
   ( %icho_% !$trace!^(!$macro!:!$altName!^)) &( %debug_% "valid()", #equ )
   :: (

   :: )

   ( %debug_% "perform()", #equ )
   :: (
      set "$vl=" &%forQ_% (

         "doskey /m:!$macro!.0"
         "doskey /m:!$macro!.1"
         "doskey /m:!$macro!.2"
         "doskey /m:!$macro!.3"

      ) do    %forLineR_% (

         '%%~?'

      ) do set "$=%%r" &set "$=!$:~3!" &if defined $ (

         ( %deq_% %necho_%$=!$!_ )
         set "$=!$:§q="!"      %=               deco     34      =%
         ( %advDecoLH_% $, /KCF )   %=               coded mark unset   =%

         set "$vl=!$vl!!$!"
      )

      ( %necho_%  !$altName!: '!$vl!' )
      set "!$altName!=!$vl!" &if defined $vl (
      
         ( %exception.clear% )
      ) else    ( %exception.set_% 1 )
   :: )
   set "$rA=!$rA!,!$altName!"
   set "$r=£e"
:: )
set "$r=!$r!" &set "$rA=!$rA!" &( %exception.nPrintL% ) &( %debug_% "outside !$trace!()", #equ, #pause ) &exit /B !£e!
:: --------------------------------------------------------------------------------------------------------------------------
:skip ()
::
goto :skip "()"
%endoftest%
:: --------------------------------------------------------------------------------------------------------------------------
:§runDosKeyMacro_ ( #$macro, #$args,#  )
:: --------------------------------------------------------------------------------------------------------------------------
:: last updated       : 2015^01^22
:: support            : naDelayed
:: languages          : N/A
::
rem ( %debugOn_% "!$trace!" ) &( %debug_% "", "inside !$trace!(!§p!)", #equ )
::
( %forP_% '$macro,"%%a"' ) &set "§p=!§p:* =!"
:: (
   ( %debug_% "valid(!$macro!:!§p!)", #equ )
   :: (
      ( %getDosKeyMacro_% !$macro!, $macro ) >nul
      ( %reDelayLH_% §p )
   :: )

   ( %debug_% "perform()", #equ ) &( %deq_% %n2echo_% $macro=!$macro!_ )
   :: (
      ( %$macro% %§p% )
   :: )
   ( %debug_% "$r=!$r!_", "$rA=!$rA!_", #equ )
   ( %deq_% %necho_% $=!$!_ )
:: )
set "$r=!$r!" &set "$rA=!$rA!" &( %debug_% "outside !$trace!()", #equ, #pause ) &exit /B !£e!
:: --------------------------------------------------------------------------------------------------------------------------
:skip ()
::
goto :skip "()"
%endoftest%
Hope it helps,

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

Re: How to execute DOSKEY macros from within a batch file

#12 Post by trebor68 » 26 May 2015 04:15

I have created the batch file Test54.bat. This batch file can run the Doskey macros.
I have not tested everything.

Here the code of Test54.bat:

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set v_macro=%1

for /f "tokens=1*" %%a in ("%*") do set par=%%b
shift

for /f "tokens=1* delims==" %%a in ('doskey /macros ^| findstr /i /b /r "%v_macro%="') do (
  set test=%%b
  set test=!test:$*=%par%!
  set test=!test:$1=%1!
  set test=!test:$2=%2!
  set test=!test:$3=%3!
  set test=!test:$4=%4!
  set test=!test:$5=%5!
  set test=!test:$6=%6!
  set test=!test:$7=%7!
  set test=!test:$8=%8!
  set test=!test:$9=%9!
  set test=!test:$T=^&!
rem  echo !test!
)
endlocal & %test%


My testing batch file Test54c.bat:

Code: Select all

@echo off
setlocal
rem The following DOSKEY commands at the promt.
rem doskey ich=for %a in ($*) do @echo %a
rem doskey ich2=echo $1 $3 $5 $7 $9 $t echo $2 $4 $6 $8 $t echo $*
echo.
doskey /macros
echo.
call d:\cmdverz\test54 ich 1 2 3 4 5 6 7 8 9 10 11 12
echo.
call d:\cmdverz\test54 ich2 1 2 3 4 5 6 7 8 9 10 11 12
echo.
echo Examples from my system:
echo.
call d:\cmdverz\test54 vg
findstr /i "a1680" *.txt
echo.
call d:\cmdverz\test54 iview ..\Girls\card_a1680.jpg


Here can see the input and output. The Doskey commands I entered directly at the prompt.

Code: Select all

D:\CMDVerz>doskey ich=for %a in ($*) do @echo %a

D:\CMDVerz>doskey ich2=echo $1 $3 $5 $7 $9 $t echo $2 $4 $6 $8 $t echo $*

D:\CMDVerz>test54c

ich2=echo $1 $3 $5 $7 $9 $t echo $2 $4 $6 $8 $t echo $*
ich=for %a in ($*) do @echo %a
VG=CD /d D:\meins\VGHD\VG\categorie
VGHDAbo=D:\meins\VGHD\VGHDAbo
VGall=CD /d D:\meins\VGHD\VGall
DBex=CD /d D:\meins\VGHD\DBex\categorie
VGex=CD /d D:\meins\VGHD\VGex\categorie
VGcl=CD /d D:\meins\VGHD\VGclassic\categorie
DB=CD /d D:\meins\VGHD\DB\categorie
IVIEW="C:\Program Files (x86)\IrfanView\i_view32" $*

1
2
3
4
5
6
7
8
9
10
11
12

1 3 5 7 9
2 4 6 8
1 2 3 4 5 6 7 8 9 10 11 12

Examples from my system:

1080p.txt:a1680
big_boobs.txt:a1680
blond.txt:a1680
CardSize.txt:a1680 22 477 "Niky / Wild Flower"
Czech_republic.txt:a1680
european.txt:a1680
gekauft.txt:a1680
Groesse160.txt:a1680
high_heels.txt:a1680
piercing.txt:a1680
shaved.txt:a1680
skirts.txt:a1680
text.txt:a1680
VGHD2014.txt:a1680

D:\CMDVerz>

Even the last command in the batch file is executed. The image viewer IrfanView runs and displays an image.

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

Re: How to execute DOSKEY macros from within a batch file

#13 Post by Ed Dyreen » 26 May 2015 06:06

To clarify my previous example ( which wasn't very clear at all, sorry :p ); the only thing required to store and retrieve variables from dosKey memory are the switches;

/exename= x = y ( store data y as variable x )
/m: x ( retrieve data y from variable x )

this way it is possible to retrieve a variable behind endlocal.

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

Re: How to execute DOSKEY macros from within a batch file

#14 Post by dbenham » 31 May 2015 19:26

Interesting idea tooComplexEd :!: :)

I couldn't follow your first post - you definitely lived up to your nickname :wink:

Your second post set me on the right path, but it was missing a few details. You still need a FOR /F to transfer the DOSKEY macro value to a normal environment variable. Unfortunately, this leads to a limitation in that ! (and possibly ^) will be corrupted if the caller has delayed expansion enabled. Previously explored safe return techniques could be adopted to preserve problematic characters.

Here is a demonstration showing how two variables could be passed across the ENDLOCAL border. This also demonstrates the limitation when delayed expansion is enabled:

Code: Select all

@echo off
setlocal disableDelayedExpansion
echo DelayedExpansion=OFF
echo --------------------
call :test
echo After Return:
set var
echo(

setlocal enableDelayedExpansion
echo DelayedExpansion=ON
echo --------------------
call :test
echo After Return:
set var
echo(

exit /b


:test
  setlocal enableDelayedExpansion

  :: Define local variables
  set "var1=Hello^world"
  set "var2="This ^& that" & the other thing^! ^^"

  echo Before return:
  set var
  echo(

  :: Save the local values for transport across ENDLOCAL
  for %%V in (var1 var2) do doskey /exename=vars %%V=!%%V!

  endlocal

  echo After ENDLOCAL:
  set var
  echo(

  :: Restore the values and undefine the DOSKEY "macros"
  for /f "delims=" %%V in ('doskey /m:vars') do (
    set "%%V"
    for /f "delims==" %%V in ("%%V") do (doskey /exename=vars %%V=)
  )
exit /b

--OUTPUT--

Code: Select all

DelayedExpansion=OFF
--------------------
Before return:
var1=Hello^world
var2="This & that" & the other thing! ^

After ENDLOCAL:
Environment variable var not defined

After Return:
var1=Hello^world
var2="This & that" & the other thing! ^

DelayedExpansion=ON
--------------------
Before return:
var1=Hello^world
var2="This & that" & the other thing! ^

After ENDLOCAL:
var1=Hello^world
var2="This & that" & the other thing! ^

After Return:
var1=Hello^world
var2="This & that" & the other thing


At first I thought this technique didn't have any advantages over previously discovered return techniques. But then I realized that the DOSKEY values are preserved even after CMD /C exits :!: This could be very useful. For example, it enables the capture of the ERRORLEVEL from both sides of a pipe :D

Code: Select all

@echo off
setlocal

:: Capture the errorlevel for both the left and right sides of pipe
(
  findstr "@" "%~f0"
  call doskey /exename=vars leftErr=%%^^errorlevel%%
) | (
  findstr "q"
  call doskey /exename=vars rightErr=%%^^errorlevel%%
)

:: Retrive the errorlevels
for /f "delims=" %%V in ('doskey /m:vars') do (
  set "%%V"
  for /f "delims==" %%V in ("%%V") do (doskey /exename=vars %%V=)
)

:: Show the results
echo  left ERRORLEVEL = %leftErr%
echo right ERRORLEVEL = %rightErr%

--OUTPUT--

Code: Select all

 left ERRORLEVEL = 0
right ERRORLEVEL = 1


Dave Benham

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

Re: How to execute DOSKEY macros from within a batch file

#15 Post by dbenham » 01 Jun 2015 13:04

npocmaka_ wrote:As the first posts in the thread are just 'Posted bellow' I'm not completely sure what the final goal is , but probably this could help:

http://ben.versionzero.org/wiki/Doskey_Macros

That just shows one method for defining DOSKEY macros - not really any help.

The original topic was exactly as the title says: "How to execute DOSKEY macros from within a batch file?" DOSKEY macros only work when issued from the command line via the keyboard. You cannot invoke a DOSKEY macro via batch code. Cleptography was claiming it could be done, but he never showed the method. I have my doubts as to whether he ever had a practical solution for modern Windows machines.

It looks to me like trebor68 is extracting commands from a defined DOSKEY macro, and then executing the commands via batch. But I don't consider that to be truly executing the macro.

My post was in response to Ed's posts regarding the use of DOSKEY macros as a memory storage unit - an alternative to files and environment variables. We basically treat the macro definition as the value for a variable, not as a macro.

The technique is of interest because DOSKEY macro definitions survive ENDLOCAL. They even survive EXIT from a child cmd.exe process as long as the parent console window remains open.


Dave Benham

Post Reply