Entering batch argument groups during execution

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Entering batch argument groups during execution

#1 Post by sambul36 » 11 Nov 2020 20:38

Below is given a snippet from a lengthy batch. In this snippet a user is expected to enter batch arguments in a simple one line sequence without further questions asked. However, it works only when each argument is represented by one value. How to make it work for an argument represented by several values, such as random device ID sequence 1,3,5,9,15 all to be considered as one "value" array? In particular how to enter the next argument after such sequence?

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "mes24=Do you want to enter batch arguments? Y/N"
set "mes25=Batch arguments (examples: s n y / t n / o 5 y / e 1 3) "
if "%1"=="" (
	choice /c yn /n /m "%mes24%" /t 10 /d n
		if !errorlevel! equ 1 (
			echo/ & set /p "camn=%mes25% > "
			if defined camn ("%~f0" !camn!)))
For example, I want to enter 3 argument groups: (e), (1 3 5 20), (y). How to do it in that sequence without making the code more complex?
Last edited by sambul36 on 14 Nov 2020 00:31, edited 4 times in total.

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

Re: Entering batch argument groups during execution

#2 Post by T3RRY » 12 Nov 2020 08:34

keeping to a similar style, here is one example of how to achieve this:

Code: Select all

@Echo off
(Set LF=^

%Do Not Modify%)
If Not "!![" == "[" Setlocal EnableExtensions EnableDelayedExpansion
:CheckArgs
If "%~1" == "" (
 Choice /N /M "Use Batch Args Y/N?"
 If !Errorlevel! Equ 1 (
  For %%n in (1 2 3)Do Set /P "Arg[%%n]=arg%%n type Description:"
 ) Else Goto :NoArgs
) Else Goto :Args
For %%n in (1 2 3)Do if "!Arg[%%n]!" == "" (
 Echo/Arg %%n missing
 Goto :CheckArgs
)
"%~f0" "%Arg[1]%" "%Arg[2]%" "%Arg[3]%"
Exit /B 0
:Args
 Echo/%*
:NoArgs
 Echo/Done

sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Re: Entering batch argument groups during execution

#3 Post by sambul36 » 13 Nov 2020 23:34

Thanks. Are any other approaches available to enter batch arguments in groups aiming for the shortest or less deviating from the above example code possible?

I found simpler approach: take an array parameter in quotes. Then process that parameter %~n as an array, which you have to do anyway:

Code: Select all

set /p "camn=%mes25% > " e "1 3 5 20" y
if defined camn ("%~f0" !camn!)
if not [%2]==[] (
	for %%k in (%~2) do ... )

Post Reply