Search code examples
batch-filecmdmenusetcall

How to use call echo in a string


I am using a menu and want to write to a file with selected name in dropdown. It's working fine except the file name is incorrect.

It's naming it " ws-%Name[2]% " and not putting the correct value in. How can I fix this this? thanks for any help

ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO Select Postroom staff:-
ECHO ...............................................
ECHO.

set "Name[1]=Diana Mckinley"
set "Name[2]=Lenka Smolkova"
set "Name[3]=Louise Smith"
set "Name[4]=Sally Baldwin"
set "Name[5]=Sally Faulkner"
set "Name[6]=Sarah Kendle"
set "Name[7]=Sophie Jennings"
set "Name[8]=Steven Caylor"

:MenuLoop
set /a "x+=1"
if defined Name[%x%] (
    call echo   %x%. %%Name[%x%]%%
    goto MenuLoop
)

ECHO.

SET /P index=Type KEY then press ENTER to register:

call echo %%Name[%index%]%% is now registered

call echo %%Name[%index%]%% > "C:\Users\karim ali\Documents\Test\ws-%%Name[%index%]%%.txt"

GOTO MenuLoop

Solution

  • All you'll need to do is to set that name and index as a variable and use that.

    Here is an example with a few changes for demonstation purposes:

    @ECHO OFF
    SET "Name[1]=Diana Mckinley"
    SET "Name[2]=Lenka Smolkova"
    SET "Name[3]=Louise Smith"
    SET "Name[4]=Sally Baldwin"
    SET "Name[5]=Sally Faulkner"
    SET "Name[6]=Sarah Kendle"
    SET "Name[7]=Sophie Jennings"
    SET "Name[8]=Steven Caylor"
    
    :MENU
    CLS
    ECHO.
    ECHO ...............................................
    ECHO Select Postroom staff:-
    ECHO ...............................................
    ECHO.
    
    :MenuLoop
    FOR /F "TOKENS=2-3 DELIMS==[]" %%A IN ('SET Name[') DO (
        CALL ECHO   %%A. %%Name[%%A]%%)
    ECHO.
    SET /P index=Type KEY then press ENTER to register:
    CALL SET "PRSName=%%Name[%index%]%%"
    ECHO.
    ECHO %PRSName% is now registered
    TIMEOUT 2 /NOBREAK 1>NUL
    ECHO %PRSName%>"C:\Users\karim ali\Documents\Test\ws-%PRSName%.txt"
    SET "Name[%index%]="
    SET Name[>NUL 2>&1&&(GOTO :MENU)