0

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 ?

akmot
  • 63
  • 8

2 Answers2

0
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:\>
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • its printing this for me `(o/n) ?ous relever le disque C:`. why ? every thing after the variable is puted at the bigining. – akmot Jul 25 '22 at 14:23
  • I suspect that your editor is saving the code as unicode or perhaps with unix line-endings. Batch files should be saved as ANSI with line endings. I use `editplus` as an editor, others use `notepad++` - these are popular selections for editors. `Notepad` or a word-processor may save in an unwanted manner. – Magoo Jul 25 '22 at 14:44
  • Ok im editing my code on pycharm and I interpret my code with cmd on windows. – akmot Jul 25 '22 at 14:50
  • im also using CRLF UTF8 – akmot Jul 25 '22 at 14:59
  • 1
    Batch files should be saved as ANSI with line endings, not UTF8. I've appended the response from my system using the above batch code, saved as ANSI with – Magoo Jul 25 '22 at 15:14
  • The biggest issue is answered so i will close the ticket, but I'm still struggling with printing after a variable. – akmot Jul 26 '22 at 07:03
0

Here's one I prepared earlier for generating menu's from command output.

The below script defines a Macro that when expanded captures the output of the subsequent command, builds the output into a list of choices for selection by a user, the value of which is returned in the variable declared as argument 1 when the macro is defined. Additional arguments can be provided to tokenize and deliminate the output - useage is described in the codes remarks.

The below script demonstrates the definition call of the macro and expansion with command relevant to your use case.

@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
T3RR0R
  • 2,747
  • 3
  • 10
  • 25