Home > OS >  How to echo a message an an array on the same line in batch?
How to echo a message an an array on the same line in batch?

Time:07-26

Im getting the list of disk on my computer :

REM lister les disques
:list_disk
    ECHO Liste des disques

    set n=0
    for /f "skip=1 delims=" %%a in ('wmic logicaldisk get DeviceID^') do (
       set nom[!n!]=%%a
       set /A n =1
    )
    set /A "N=%n%/2"
    FOR /L %%i IN (0,1,%N%) DO  (
    echo !nom[%%i]!
    echo/

    )
EXIT /B 0

As you can see the discks ID are in an array.

Now I want to give the oportunity to the scipt user to chose or not a disk.

REM choix des disque
:choix nom
    echo choix
    set n=0
    set /A "N=%n%/2"
    SET c=n
    FOR /L %%i IN (0,1,%N%) DO  (
       echo voulez vous relever le disque !nom[%%i]! (o/n) ?

       SET /P c=
       if c==o
       rem call :HASH !nom[%%i]!

EXIT /B 0

Im trying to echo message !nom[%%i]!

but its doesn't work.

How would you do ?

CodePudding user response:

Here's one I prepared earlier.

@Echo off & Setlocal DisableDelayedExpansion
 CLS

Rem Arg 1 defines macro suffix for expansion and return variable. Args 2   3 override default macro token and delim definition.
 Call:DefMenuMacro [WMICdrive] 2 "="

 %Menu[WMICdrive]%wmic logicaldisk get DeviceID /Value
 Set [WMICdrive]

 Pause

Endlocal & Goto:Eof

======================================================================
:DefMenuMacro <MENUNAME> [tokens] [Delims]

If "%~2" == "" (
    set "Menu.Tokens=*"
)Else set "Menu.Tokens=%~2"

If "%~3" == "" (
    set "Menu.Delims="
)Else set "Menu.Delims=%~3"

(Set \n=^^^

%= Newline var \n for multi-line macro definition - Do not modify. =%)
 Set LF=^


Rem above empty lines required

:# Menu macro Definition requires environment with DelayedExpansion DISabled
 If "!!" == "" Exit /B 1

:# Key index list Allows 36 menu Menu.Options. Component of Menu Macro
 Set "Menu.List=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

:# Menu macro Usage: %MenuMENUNAME%command
==== Set Menu%~1=For %%n in (1 2)Do if %%n==2 (%\n%
%= Output Dividing Line                        =%  If not defined Menu.First Echo(!Menu.Div!%\n%
%= Reset Menu.opt# index value for Menu.Opt[#] =%  Set "Menu.opt#=0"%\n%
%= Undefine choice option key list             =%  Set "Menu.Chars="%\n%
%= For Each in list;                           =%  For /F "UsebackQ Tokens=%Menu.Tokens% Delims=^%Menu.Delims%" %%F in (`!Command.Output!`)Do For /f "Delims=" %%G in ("%%F")Do If not "%%~G" == "!LF!" (%\n%
%= For Option Index value                      =%   For %%i in (!Menu.opt#!)Do IF not %%i GTR 35 (%\n%
%= Build the Choice key list and Menu.Opt[#]   =%    Set "Menu.Chars=!Menu.Chars!!Menu.List:~%%i,1!"%\n%
%= array using the character at the            =%    Set "Menu.Opt[!Menu.List:~%%i,1!]=%%~G"%\n%
%= current substring index.                    =%    Set "Menu.Display=%%~G"%\n%
%= Display ; removing # variable prefix        =%    Echo([!Menu.List:~%%i,1!] !Menu.Display:#=!%\n%
%= Increment Menu.Opt[#] Index var 'Menu.opt#' =%    Set /A "Menu.opt# =1"%\n%
%= Close Menu.opt# loop                        =%   )%\n%
%= Close Menu.options loop                     =%  )%\n%
%= Output Menu.Dividing Line                   =%  Echo(!Menu.Div!%\n%
%= Select option by character index            =%  For /F "Delims=" %%o in ('Choice /N /C:!Menu.Chars!')Do (%\n%
%= Assign return var 'OPTION' with the         =%   Set "%~1=!Menu.Opt[%%o]!"%\n%
%=                                             =%  )%\n%
%= Return option accross endlocal              =%  For %%v in ("!%~1!")Do Endlocal ^& (%\n%
                                                    Set "%~1=%%~v"%\n%
                                                   )%\n%
%= Capture Macro input - Menu.Options List     =% )Else Setlocal ENABLEEXTENSIONS EnableDelayedExpansion ^& Set Command.Output=
========================================== ::: End Menu Definition
Exit /b 0

CodePudding user response:

FOR /L %%i IN (0,1,%N%) DO  (
   echo voulez vous relever le disque !nom[%%i]! (o/n^) ?

   SET c=n
   SET /P c=
   if !c!==o ECHO call :HASH !nom[%%i]!
)

should cure your problem (as I see it)

The caret ^ must be included to "escape" the ) otherwise the ) in the message will close the do (.

And then you're missing a ) to close the do (. You need to set c to n each time you prompt otherwise it will retain its value from when it was last set.

The if statement is incomplete and must attempt to match !c! which is the value of variable c after the set has been responded to.

c will never be equal to o.

You can use if /i... to make the comparison case-insensitive.

Please consider choice which is designed for this task. Use the search facility for [batch-file] choice eg Gerhard's example https://stackoverflow.com/a/58370914/2128947 or see the documentation - choice /? from the prompt.


Run log

voulez vous relever le disque Q (o/n) ?
o
call :HASH Q
voulez vous relever le disque W (o/n) ?
n
voulez vous relever le disque X (o/n) ?
o
call :HASH X
U:\>
  • Related