如何在字符串中使用call echo

时间:2016-10-21 10:21:08

标签: batch-file cmd menu set call

我正在使用菜单,并希望在下拉列表中写入具有所选名称的文件。它工作正常,但文件名不正确。

它将其命名为“ws-%Name [2]%”并且没有输入正确的值。 我该如何解决这个问题?谢谢你的帮助

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

2 个答案:

答案 0 :(得分:1)

您需要做的就是将该名称和索引设置为变量并使用它。

以下是一个示例,为演示目的进行了一些更改:

@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)

答案 1 :(得分:0)

嵌套变量的扩展可能令人困惑......

而不是ws-%%Name[%index%]%%.txt,请写ws-%Name[%%index%%]%.txt