Use Checklist to Run Selected Commands.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Use Checklist to Run Selected Commands.

#1 Post by PAB » 12 Jan 2022 13:05

Good evening,

A VERY Happy New Year to one and all.

I have a .bat file that runs several commands one after the other. If I do not want to run a particular Command[s], I have to manually comment out that/those Commands before running the Script.

With this in mind, I decided to try and setup a checklist where I could select ONLY those that I wanted to run.

I have spent hours trying to investigate this [ I do not want to use PowerShell ], and I have come up with the following which I found and have slightly amended.

My intention is to obviously add this to the top of my existing code.

How can I now get it to run after the selection[s] have been made please?

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "num=0"
for %%a in ("Option 1"
            "Option 2"
            "Option 3"
            "Option 4"
            "Option 5"
            "Option 6"
            "Option 7"
            "Option 8"
            "Option 9"
            ) do (
  set /A num+=1
  set "msg[!num!]=%%~a"
  set "opt[!num!]= "
)

set "digits=0123456789"

:select
  (
  cls
  echo. & echo Enter the actual Number to Check/Uncheck an Option, or X to EXIT . . . & echo.
  for /L %%i in (1,1,%num%) do echo !digits:~%%i,1!. [!opt[%%i]!] !msg[%%i]!
  )
  choice /C !digits:~1,%num%!X /N > nul
  if %ErrorLevel% gtr %num% goto :endSelect
  set "sel=%ErrorLevel%"
  if "!opt[%sel%]!" equ "X" (set "opt[%sel%]= ") else set "opt[%sel%]=X"
  goto :select
:endSelect

echo. & echo.
for /L %%i in (1,1,%num%) do (
  if "!opt[%%i]!" equ "X" (
    echo %%i selected
  )
)
I would be VERY grateful for help please.

Thanks.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Use Checklist to Run Selected Commands.

#2 Post by Compo » 12 Jan 2022 18:53

Why not just simplify the process?

Code: Select all

%SystemRoot%\System32\choice.exe /M "Would you like to run Command1"
If Not ErrorLevel 2 Command1
%SystemRoot%\System32\choice.exe /M "Would you like to run Command2"
If Not ErrorLevel 2 Command2
%SystemRoot%\System32\choice.exe /M "Would you like to run Command3"
If Not ErrorLevel 2 Command3
%SystemRoot%\System32\choice.exe /M "Would you like to run Command4"
If Not ErrorLevel 2 Command4
%SystemRoot%\System32\choice.exe /M "Would you like to run Command5"
If Not ErrorLevel 2 Command5
…
That way, you never need to make modifications, the decisions are made at run time.

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

Re: Use Checklist to Run Selected Commands.

#3 Post by Squashman » 12 Jan 2022 21:37

If you want to do something really fancy look at the 5th screenshot in this post
viewtopic.php?t=6581#p42362

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Use Checklist to Run Selected Commands.

#4 Post by PAB » 13 Jan 2022 05:36

Thank you both for your answers, it is appreciated.

I tried Compo's method and it doesn't do what I want it to do. I adapted the code by adding some dummy Commands, but if I enter 'Y' for one of the Options, it runs ALL the Options below that Option automatically.

Code: Select all

@echo off

%SystemRoot%\System32\choice.exe /M "Would you like to run Command1"
If Not ErrorLevel 2 goto :Command1
%SystemRoot%\System32\choice.exe /M "Would you like to run Command2"
If Not ErrorLevel 2 goto :Command2
%SystemRoot%\System32\choice.exe /M "Would you like to run Command3"
If Not ErrorLevel 2 goto :Command3
%SystemRoot%\System32\choice.exe /M "Would you like to run Command4"
If Not ErrorLevel 2 goto :Command4

:Command1
mspaint.exe

:Command2
Notepad.exe

:Command3
perfmon.exe

:Command4
"C:\Users\System-Admin\Desktop\DEFINATELY DO.txt"

pause & Exit
As far a Squashman's recommendation, this is FAR above my level. I am not a programmer or anything like that. I just like dabbling with little Scripts that I use for personal use on my OS.

Like in my original Post, I would like to check/mark those that I want to run and then hit a key for it to ONLY run the Commands selected.

Thank you BOTH again.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Use Checklist to Run Selected Commands.

#5 Post by ShadowThief » 13 Jan 2022 12:56

PAB wrote:
13 Jan 2022 05:36
Thank you both for your answers, it is appreciated.

I tried Compo's method and it doesn't do what I want it to do. I adapted the code by adding some dummy Commands, but if I enter 'Y' for one of the Options, it runs ALL the Options below that Option automatically.

Code: Select all

@echo off

%SystemRoot%\System32\choice.exe /M "Would you like to run Command1"
If Not ErrorLevel 2 goto :Command1
%SystemRoot%\System32\choice.exe /M "Would you like to run Command2"
If Not ErrorLevel 2 goto :Command2
%SystemRoot%\System32\choice.exe /M "Would you like to run Command3"
If Not ErrorLevel 2 goto :Command3
%SystemRoot%\System32\choice.exe /M "Would you like to run Command4"
If Not ErrorLevel 2 goto :Command4

:Command1
mspaint.exe

:Command2
Notepad.exe

:Command3
perfmon.exe

:Command4
"C:\Users\System-Admin\Desktop\DEFINATELY DO.txt"

pause & Exit
As far a Squashman's recommendation, this is FAR above my level. I am not a programmer or anything like that. I just like dabbling with little Scripts that I use for personal use on my OS.

Like in my original Post, I would like to check/mark those that I want to run and then hit a key for it to ONLY run the Commands selected.

Thank you BOTH again.
You'll need an exit command after each of the executables in order for the script to stop running. Labels are just markers for the interpreter; they don't actually sandbox off parts of the code.

Also, if you were going to follow Compo's suggestion, your code would look like this:

Code: Select all

@echo off
%SystemRoot%\System32\choice.exe /M "Would you like to run Command1"
If Not ErrorLevel 2 mspaint.exe
%SystemRoot%\System32\choice.exe /M "Would you like to run Command2"
If Not ErrorLevel 2 Notepad.exe
%SystemRoot%\System32\choice.exe /M "Would you like to run Command3"
If Not ErrorLevel 2 perfmon.exe
%SystemRoot%\System32\choice.exe /M "Would you like to run Command4"
If Not ErrorLevel 2 "C:\Users\System-Admin\Desktop\DEFINATELY DO.txt"
pause
exit
Based on the fact that you've added gotos and labels for seemingly no reason, I'm going to assume that you intend to run more than one command per choice, which would mean your code needs to look like this:

Code: Select all

@echo off
%SystemRoot%\System32\choice.exe /M "Would you like to run Command1"
If Not ErrorLevel 2 (
	mspaint.exe
)
%SystemRoot%\System32\choice.exe /M "Would you like to run Command2"
If Not ErrorLevel 2 (
	Notepad.exe
)
%SystemRoot%\System32\choice.exe /M "Would you like to run Command3"
If Not ErrorLevel 2 (
	perfmon.exe
)
%SystemRoot%\System32\choice.exe /M "Would you like to run Command4"
If Not ErrorLevel 2 (
	"C:\Users\System-Admin\Desktop\DEFINATELY DO.txt"
)
pause
exit
Last edited by ShadowThief on 13 Jan 2022 13:00, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Use Checklist to Run Selected Commands.

#6 Post by Compo » 13 Jan 2022 12:59

Apologies, my example was really to show you the choice use per command, however, to use it as you appear to be doing, you should not use GoTo, but use Call instead:

Example:

Code: Select all

@Echo Off
%SystemRoot%\System32\choice.exe /M "Would you like to open Paint"
If Not ErrorLevel 2 Call :Command1
%SystemRoot%\System32\choice.exe /M "Would you like to open Notepad"
If Not ErrorLevel 2 Call :Command2
%SystemRoot%\System32\choice.exe /M "Would you like to run PerfMon"
If Not ErrorLevel 2 Call :Command3
If Exist "%UserProfile%\Desktop\DEFINITELY DO.txt" (
	%SystemRoot%\System32\choice.exe /M "Would you like to read DEFINITELY DO.txt"
	If Not ErrorLevel 2 Call :Command4
)
Pause
GoTo :EOF

:Command1
%SystemRoot%\System32\mspaint.exe
Exit /B

:Command2
%SystemRoot%\System32\notepad.exe
Exit /B

:Command3
%SystemRoot%\System32\perfmon.exe
Exit /B

:Command4
"%UserProfile%\Desktop\DEFINITELY DO.txt"
Exit /B

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Use Checklist to Run Selected Commands.

#7 Post by PAB » 13 Jan 2022 15:03

I would just like to thank EVERYONE so much for their help on this.

The above TWO codes do indeed run the Commands that I used just as an example. However, they both open the Commands and will NOT carry on until the Programs are closed, hence, I need to be at the computer until ALL the Commands have run and been closed.

What I ideally want, is to be able to initially just select the Commands that I DO want to run and ignore the Commands that I do NOT want to run, and then hit a key to start the run of ALL the specified selections, obviously waiting for each Command to finish before it runs the next. This way, I can leave the computer running without any user intervention, hence the code I initially posted.

Here is a small selection [ there are quite a few more ] to show what sort of things I will be running . . .

Code: Select all

@echo off

set "Dism=%SystemRoot%\System32\Dism.exe"
set "CBS_ALL=%WinDir%\Logs\CBS\*.*"
set "CBS_log=%WinDir%\Logs\CBS\CBS.log"
set "CBS_Persist_cab=%WinDir%\Logs\CBS\CbsPersist*.cab"
set "CBS_Persist_log=%WinDir%\Logs\CBS\CbsPersist*.log"
set "CBS_Persist_txt=%WinDir%\Logs\CBS\CbsPersist*.txt"
set "DIS_log=%WinDir%\Logs\DISM\DISM.log"
set "TSH=%LocalAppData%\ElevatedDiagnostics"

:Component
echo. & echo  ######################################################################################################################################################################
        echo  #                                                                                                                                                                    #
        echo  #  Component Store - Cleanup the [ %WinDir%\WinSxS ] Component Store folder:-                                                                                      #
        echo  #                                                                                                                                                                    #
        echo  ######################################################################################################################################################################
echo. & echo     Processing . . .
                 taskkill /im TiWorker.exe /f 1>nul 2>nul
                 taskkill /im TrustedInstaller.exe /f 1>nul 2>nul
                 %Dism% /Online /Cleanup-Image /StartComponentCleanup
echo. & echo     Processing Complete.

:TroubleShooting
echo. & echo  ######################################################################################################################################################################
        echo  #                                                                                                                                                                    #
        echo  #  TroubleShooting History - Delete the [ %TSH% ] folder:-                                                         #
        echo  #                                                                                                                                                                    #
        echo  ######################################################################################################################################################################
echo. & echo     Processing . . .
                 if exist %TSH% (rmdir /s /q %TSH% >nul 2>&1)
        echo     Processing Complete.

:CBS
echo. & echo  ######################################################################################################################################################################
        echo  #                                                                                                                                                                    #
        echo  #  CBS - Delete ALL [ %WinDir%\Logs\CBS\ ] files:-                                                                                                                 #
        echo  #                                                                                                                                                                    #
        echo  ######################################################################################################################################################################
echo. & echo     Processing . . .
                 if exist %CBS_ALL% (del /f /q %CBS_ALL% >nul 2>&1)
        echo     Processing Complete.

:DISM
echo. & echo  ######################################################################################################################################################################
        echo  #                                                                                                                                                                    #
        echo  #  DISM - Delete the [ %DIS_Log% ] file:-                                                                                                        #
        echo  #                                                                                                                                                                    #
        echo  ######################################################################################################################################################################
echo. & echo     Processing . . .
                 if exist %DIS_log% (del /f /q %DIS_log% >nul 2>&1)
        echo     Processing Complete.
echo. & echo  ================================================================================ EOF =================================================================================
echo. & goto :REBOOT_Choice

:REBOOT_Choice

CHOICE /C:YN /M ">Do you want to REBOOT NOW" 
if %ErrorLevel%==2 goto :No
if %ErrorLevel%==1 goto :Yes

:Yes
cls & color A & echo. & echo  REBOOTING NOW . . . & timeout /t 3 /nobreak >nul & shutdown /r /f /t 00

:No
Exit /b
I hope I have explained this a bit clearer.

If this is not possible, then please just say, as there are other members who probably have a greater need for your expert help than myself, as this is just for personal use, whereas theirs maybe for work.

Thank you.

T3RRY
Posts: 249
Joined: 06 May 2020 10:14

Re: Use Checklist to Run Selected Commands.

#8 Post by T3RRY » 13 Jan 2022 19:33

each of those command menus in the script you've posted above currently flow through to the following command
To achieve the type of script flow you describe, each comand set needs to be used as a function via call and terminated using Exit /b or goto:eof
The all you need to do is build a list of function labels to be called using whatever selection method you settle on.

A bare bones example:

Code: Select all

@Echo off


Setlocal EnableDelayedExpansion
 Set "Commands="

Echo(Confirm Selections
 For %%G in (
  "Some Command        [1]"
  "Another Command     [2]"
  "Yet another Command [3]"
 )Do (
  Set "Option=%%~G"
  Echo(%%~G
  Choice.exe /C:YN
  If not errorlevel 2 Set "Commands=!Commands! !Option:*[=[!"
 )
 For %%G in (!Commands!)Do Call:Option%%G

Endlocal & Goto:eof


:Option[1]
 Echo(--1
Exit /b 0

:Option[2]
 Echo(--2
Exit /b 0

:Option[3]
 Echo(--3
Exit /b 0

Post Reply