How can I use a defined menu type list for running a range of selections, i.e. conditionally tick off the selected items at once.
Conventionally, the configurator looks like this:
@echo off
chcp 1251 >nul
:begin
echo [1] Folder 1 [3] Folder 3
echo [2] Folder 2 [4] Folder 4
set /P op=Enter the number:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3
if "%op%"=="4" goto op4
:op1
cls
call "Delete_Folder1.bat" >nul
echo Done
timeout /t 2 >nul
cls
goto begin
:op2
cls
call "Delete_Folder2.bat" >nul
echo Done
timeout /t 2 >nul
goto begin
:op3
cls
call "Delete_Folder3.bat" >nul
echo Done
timeout /t 2 >nul
cls
goto begin
:op4
cls
call "Delete_Folder4.bat" >nul
echo Done
timeout /t 2 >nul
cls
goto begin
I don't understand how to select multiple items at once. Conditionally 1 and 3, or 2, 3 and 4, That is, to list several items in a row, and have them all fulfilled.
The simple solution would be using a fail-safe and secure single choice solution with the Windows command CHOICE.
A more difficult solution is a multiple choice solution using
set /P.A prompt of the user for input with
set /Pgives a user the freedom to enter anything from an empty string to an intentionally completely wrong string or a by mistake typed wrong string.See: How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? This answer describes very detailed both techniques used in the two batch files above.
The following command line in second batch file needs some extra explanation:
It is necessary to reference the value of the environment variable
UserChoiceenclosed in"for getting all characters of user input string interpreted literally including characters like&<>|being otherwise interpreted as operators before the string is output with commandechowhich outputs also the two double quotes. See: How does the Windows Command Interpreter (CMD.EXE) parse scripts?The user input string enclosed in
"is redirected as input tofindstrwhich runs a case-sensitive regular expression search with interpreting the space character as literal character because of using the options/Rand/C:. The regular expression search is positive only on the input string"as written in the batch file,"as written in the batch file.There is used
1234and not1-4in the two square brackets as the latter would result in matching also¹²³which is not acceptable in this case.It is important here that no space is left to the redirection operator
|because ofechowould also output that space andfindstrwould never find a positive match with the specified expression on trailing space output byecho.The input validation done with
findstrresults in an exit with value1meaning no match if the user enters a string like1, 6, 08or1, <batch> & test, 2in which case the user is prompted once again.But an input like
1,3 4, 2 11,, ,32 4321 444444444444444444444results in a positive find with exit code0and the FOR loop is run next with calling:The numbers
11,32,4321,444444444444444444444are ignored because of the two IF conditions.The second batch file does not prompt the user a second time because the user has the freedom to enter all four numbers at once on prompt.
Both batch files determine the current code page before changing it to Windows-1251 and restore that code page before exiting. That is recommended for any batch file which changes the default code page in case of the user runs the batch file from within a command prompt window instead of double clicking on it in Windows File Explorer and the user continues using the command prompt with expecting usage of default code page after batch file execution finished. See Saving current codepage on DosTips for more details.
See also:
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?chcp /?choice /?cls /?echo /?endlocal /?findstr /?for /?goto /?if /?set /?setlocal /?timeout /?