How to execute DOSKEY macros from within a batch file
Posted: 08 Jun 2011 20:25
POSTED BELOW:
A Forum all about DOS Batch
https://www.dostips.com/forum/
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
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
Cleptography wrote:It didn't work for you
That's odd works for me...
I wonder what that little bit does
Anyone else not working
Code: Select all
( echo doskey xx=echo YY& echo doskey /macros & echo xx & echo doskey /macros & echo exit) | (%COMSPEC% echo hello )
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..
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% )
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%
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%
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
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>
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
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
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%
Code: Select all
left ERRORLEVEL = 0
right ERRORLEVEL = 1
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