0

Thanks for looking into my concern.

I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.

Could anyone help me with some logic here. So that, I can take it forward.

Thank you.

Ras
  • 543
  • 1
  • 9
  • 25
  • See [this page](https://stackoverflow.com/q/15885132/1683264). If you prefer to keep the choice in the console, you might also be interested in [this sort of menu](https://stackoverflow.com/a/34983868/1683264). Populate the `menu[]` array with a `for` loop. `setlocal enabledelayedexpansion` and `set "idx=0"` then `for %%I in (*.config) do ( set "menu[!idx!]=%%~nxI" && set /a idx += 1 )` to build a menu of all files matching *.config – rojo Aug 03 '17 at 01:52
  • Show us your code? – lit Aug 03 '17 at 02:14

1 Answers1

1

A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:

FOR
SET /P
IF EXIST

Good luck!

@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=
  • Thank you Steven. Now I have to make ammendments like, extension of the file should not be appeared in prompt and user should not type all the file name. he has to select options like 1,2 etc. Thank you so much for your valuable to input. – Ras Aug 03 '17 at 17:52
  • You are more than welcome. Drop the "x" from the `%%~nxF` portion of the `FOR` loop to get rid of the file extension; likewise, add it back in when referencing the filename. – Steven K. Mariner Aug 04 '17 at 01:04