Using Doskey macro to run Choice

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Using Doskey macro to run Choice

#1 Post by Quisquose » 26 Jul 2017 05:24

I am trying to learn a bit more about the very basics of batch files, so I am messing around with a simple batch that I am testing as an autorun script for an elevated privileges cmd prompt window.

The autorun batch file is basically just a few commands that run on startup of the command window (in order to configure that session). However, I also have a "Choice" section of code in there, and I would like that section of code to run only when a certain Doskey macro is typed in, but not at any other time.

I've tried various options to have that section of code skipped until it is invoked by a designated macro, but it still runs at startup every time.

What would be the best way to achieve this?

Below is the autorun batch file that I'm testing. All attempts to constrain the choice code have been removed from it (because none of them worked, and they were a mess).

Thanks!

Code: Select all

@ECHO Off
TITLE SUPER USER
CD C:\Windows
Color 4F
@whoami /user /FO list
set prompt=[Root] $p$g

:choice
echo.
echo.
::define a variable containing a single backspace character (to allow spaces to display at beginning of line)
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set /p c=%BS%  Start a new command session?  [Y/N]
if /I "%c%" EQU "Y" goto :proceed
if /I "%c%" EQU "N" goto :notproceed
goto :choice

:proceed
cmd /E:ON /k c:\su.cmd
goto :end

:notproceed
@echo.
@echo.
goto :end

:end

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using Doskey macro to run Choice

#2 Post by penpen » 26 Jul 2017 23:10

I'm not fully sure what you are looking for, but this might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

:: check for doskey macro "cla"; sample
:: doskey cla=echo Ok.
set "claMacroFound="
for /F "tokens=* delims=" %%a in ('doskey /macros:"cmd.exe" ^| findstr "^cla="') do set "claMacroFound=true"

if defined claMacroFound call :choice
echo Do something else.
goto :eof

:choice
echo Executed ":choice".
goto :eof


penpen

Edits:
Removed (untested);
removed the doublequotes around the variable name within the if defined variable (bugfix);
disabled delayed expansion (bugfix for macros containing exclamation mark(s));
replaced "myMacro" with "cla" (description bugfix)
replaced "certainDoskeyMacro" with "claDoskeyMacro" (name change of macro);
added sample code to choice.

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Using Doskey macro to run Choice

#3 Post by Quisquose » 14 Aug 2017 19:19

Thanks for your reply, but I'm having trouble understanding it.

To clarify what I am trying to achieve:

In the batch file that I showed in the first message of this thread, I want only the code that is before/above the :choice command to run by default when the batch file is run (the rest of the code should be ignored until such time as a certain doskey macro is types).

The :choice section of the batch file should be run only when the doskey macro cla is typed into the command window.

So, in summary: if cla is typed then the choice section of code is executed, but if cla is not typed then the choice code is not executed.

Hope that makes things clearer.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Using Doskey macro to run Choice

#4 Post by thefeduke » 14 Aug 2017 21:22

In response to your clarification, try:

Code: Select all

@ECHO Off
TITLE SUPER USER
CD C:\Windows
Color 4F
@whoami /user /FO list
set prompt=[Root] $p$g

call Set "SuperUser=%~dp0Super2User.bat"
doskey cla=Call "%SuperUser%"

Exit /B
That script can be called anything, but the way that I coded it, this one should be in the same folder and called Super2User.bat:

Code: Select all

@ECHO Off
:choice
echo.
echo.
::define a variable containing a single backspace character (to allow spaces to display at beginning of line)
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set /p c=%BS%  Start a new command session?  [Y/N]
if /I "%c%" EQU "Y" goto :proceed
if /I "%c%" EQU "N" goto :notproceed
goto :choice

:proceed
cmd /E:ON /k c:\su.cmd
goto :end

:notproceed
@echo.
@echo.
goto :end

:end

John A.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using Doskey macro to run Choice

#5 Post by penpen » 15 Aug 2017 03:54

There were some bugs in the above code: Sorry for that.
I've corrected the above example and it now should run fine.

Here is a demo usage of how i understood your task using my (edited) above code (test.bat"):

Code: Select all

Z:\>doskey /macros:"cmd.exe"

Z:\>test.bat
Do something else.

Z:\>doskey cla=echo Ok.

Z:\>doskey /macros:"cmd.exe"
cla=echo Ok.

Z:\>test.bat
Executed ":choice".
Do something else.

Z:\>
If and only if a macro named cla is defined, then the testfile calls (executes) the ":choice" section, else not.


penpen

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Using Doskey macro to run Choice

#6 Post by thefeduke » 15 Aug 2017 08:18

I streamlined my last suggestion. Now there is no second script and you may name it anything you wish:

Code: Select all

@ECHO Off
TITLE SUPER USER
CD C:\Windows
Color 4F
@whoami /user /FO list
set prompt=[Root] $p$g

call Set "SuperUser=%~f0"
doskey cla=Call "%SuperUser%" cla

If /I "%~1." NEQ "cla." Exit /B

:choice
echo.
echo.
::define a variable containing a single backspace character (to allow spaces to display at beginning of line)
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set /p c=%BS%  Start a new command session?  [Y/N]
if /I "%c%" EQU "Y" goto :proceed
if /I "%c%" EQU "N" goto :notproceed
goto :choice

:proceed
cmd /E:ON /k c:\su.cmd
goto :end

:notproceed
@echo.
@echo.
goto :end

:end
I tried to leave as much of your code as-is as I could, but I prefer

Code: Select all

pushd %systemroot%
over

Code: Select all

CD C:\Windows

John A.

Post Reply