2017-08-03 46 views
0

感谢您关注我的问题。批处理脚本读取目录中的文件名并要求用户选择一个

我在给定的目录中有五个配置文件。在我的批处理脚本中,我想读取这些文件名并将其提示给用户。一旦用户选择了一个配置文件,从它读取变量。

任何人都可以帮我在这里的一些逻辑。所以,我可以继续前进。

谢谢。

+0

请参阅[本页](https://stackoverflow.com/q/15885132/1683264)。如果您更愿意在控制台中保留选项,您可能也对[此类菜单]感兴趣(https://stackoverflow.com/a/34983868/1683264)。用'for'循环填充菜单[]'数组。 (* .config)do(set“menu [!idx!] = %%〜nxI”&& set/a idx + = 1)的'setlocal enabledelayedexpansion'和'set“idx = 0” '建立一个匹配* .config – rojo

+0

的所有文件的菜单向我们显示您的代码? – lit

回答

1

这样的批处理文件或.cmd这样的文件演示了菜单技术(没什么奇怪的,用户必须准确输入文件名)。关键项目:

FOR 
SET /P 
IF EXIST 

祝你好运!

@echo off 

REM Show the user the list and ask them which one to use 
echo. 
echo Please select one of: 
echo. 
for %%F in ("D:\A Given Directory\*.config") do echo  %%~nxF 
echo. 
set SEL_CFGFNM= 
set /P SEL_CFGFNM=Which configuration file: 

REM Make sure they answered, and that the file exists 
if "%SEL_CFGFNM%" == "" goto ENDIT 
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG 

REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists 
REM Do whatever you want to do with that file now 

REM Don't fall through the exit messages 
goto ENDIT 

REM Exit Messages 
:NOCFG 
echo. 
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list 
echo. 
goto ENDIT 

REM Cleanup 
:ENDIT 
set SEL_CFGFNM= 
+0

谢谢史蒂文。 现在我已经提出修改了, 扩展名的文件不应该出现在提示符下,并且 用户不应该输入所有的文件名。他必须选择1,2等选项。 非常感谢您的宝贵意见。 –

+0

你是多么欢迎。从'FOR'循环的'%%〜nxF'部分放下“x”以摆脱文件扩展名;同样,在引用文件名时将其添加回来。 –

相关问题