How to store multiple selections from menu prompts?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

How to store multiple selections from menu prompts?

#1 Post by SIMMS7400 » 05 Aug 2016 12:19

Good Afternoon Team -

A portion of a script I am writing prompts the user to select all directories that are required for their specific process Most times, the user will need to select multiple directories, not just one.

Then, later on the script, those directories the user selected will need to be copied to a another location. What is the best way to do this? I can do it easily if it was only one directory. As shown here:

Code: Select all

echo ***************************************************
echo RETRIEVING CONTENTS OF LCM import_export DIRECTORY
echo ---------------------------------------------------
echo -- This may take 1 minute --
echo ---------------------------------------------------
for /d /r "C:\" %%a in (*) do if /i "%%~nxa"=="import_export" set "IMP_EXP_DIR=%%a"

DIR %IMP_EXP_DIR%
echo.
PAUSE

:REDO_MIG_DEF
echo ********************************************************
echo WHAT IS THE NAME OF YOUR MIGRATION DEFINITION DIRECTORY?
echo --------------------------------------------------------
echo.
set /p MIG_DEF_DIR=
echo.
echo.
echo IS "%MIG_DEF_DIR%" CORRECT?
echo.
echo.
echo [1] Yes
echo.
echo [2] No
echo.

set /p MIG_DEF_Q=
IF %MIG_DEF_Q%==1 GOTO APP_NAME
IF %MIG_DEF_Q%==2 GOTO REDO_MIG_DEF


Then later on, the directory is copied:

Code: Select all

echo ************************************************************
echo CREATING NEW MIGRATION DEFINITION FOR %NEW_APP_NAME1% IMPORT
echo ------------------------------------------------------------
echo.
TIMEOUT /T 3 >nul

ECHO d | XCOPY /d /e /y %LCM_IE_PATH%\%MIG_DEF_DIR% %LCM_IE_PATH%\%NEW_APP_MIG_DEF% 2 >nul


In conclusion, how do I capture more than 1 selection and then use those selections in a copy command?

Thanks!

aGerman
Expert
Posts: 4717
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to store multiple selections from menu prompts?

#2 Post by aGerman » 05 Aug 2016 13:04

The only selection I see is either 1 (for Yes) or 2 (for No). What are you talking about?

Regards
aGerman

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to store multiple selections from menu prompts?

#3 Post by SIMMS7400 » 05 Aug 2016 13:46

AGerman -

That is just a question to prompt the user to make SURE the selection they entered in correct.

Their selection is currently stored in the variable called "MIG_DEF_DEF, shown as set /p MIG_DEF_DIR=

They make their selection from a list thats generated with the former section, as shown here:

Code: Select all

echo ***************************************************
echo RETRIEVING CONTENTS OF LCM import_export DIRECTORY
echo ---------------------------------------------------
echo -- This may take 1 minute --
echo ---------------------------------------------------
for /d /r "C:\" %%a in (*) do if /i "%%~nxa"=="import_export" set "IMP_EXP_DIR=%%a"


The above command outputs all the sub-directories they can chose from from in the directory called "import_export".

Since there can be more than one they need to copy elsewhere, I need a way to store multiple selections.

aGerman
Expert
Posts: 4717
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to store multiple selections from menu prompts?

#4 Post by aGerman » 05 Aug 2016 14:02

Sorry I missed the first SET /P.
How I would solve it:

Code: Select all

@echo off &setlocal
set /p "MIG_DEF_DIR=Comma separated directories: "
if defined MIG_DEF_DIR for %%i in (%MIG_DEF_DIR%) do (
  echo "%%~i"
)
pause

Code: Select all

Comma separated directories: C:\users,"C:\program files"
"C:\users"
"C:\program files"
Drücken Sie eine beliebige Taste . . .

Just use "%%~i" in your copy command.

Regards
aGerman

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to store multiple selections from menu prompts?

#5 Post by SIMMS7400 » 05 Aug 2016 14:32

Thank you aGerman.

Ill adapt this to my code and let you know. I may have another question in a bit, but will let you know.

Thank you!

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: How to store multiple selections from menu prompts?

#6 Post by douglas.swehla » 05 Aug 2016 15:51

Users may not get the list completely right the first time. Expanding on aGerman's code and your error-checking steps gives something like

Code: Select all

@echo off &setlocal

:get_dirs
set /p "new_dirs=Comma separated directories: "
set "all_dirs=%all_dirs%, %new_dirs%"

:confirm
echo "Is '%all_dirs%' the full, correct list of directories?"
echo [1] Yes.
echo [2] No, add more.
echo [3] No, start over.

set /p status=
if %status%==1 goto next_step
if %status%==2 goto get_dirs
if %status%==3 (set "all_dirs=" & goto get_dirs)
(echo You gave an invalid response.) & goto confirm

:next_step
if defined all_dirs for %%i in (%all_dirs%) do (
  echo Do stuff with directory "%%~i".
)

pause
endlocal
exit /b

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to store multiple selections from menu prompts?

#7 Post by SIMMS7400 » 05 Aug 2016 16:12

Hi Guys -

Thank you so much! This works as expected.

However, the portion where I need to perform the copy happens later on in the script, not necessarily within the block.

Is there anyway to store %%~i so I can use it later on in the script?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to store multiple selections from menu prompts?

#8 Post by Squashman » 05 Aug 2016 16:32

SIMMS7400 wrote:Hi Guys -

Thank you so much! This works as expected.

However, the portion where I need to perform the copy happens later on in the script, not necessarily within the block.

Is there anyway to store %%~i so I can use it later on in the script?

Isn't that what Doug's code does already.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to store multiple selections from menu prompts?

#9 Post by SIMMS7400 » 05 Aug 2016 16:35

Ah yes, testing now!

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: How to store multiple selections from menu prompts?

#10 Post by douglas.swehla » 05 Aug 2016 16:40

SIMMS7400 wrote:. . .the portion where I need to perform the copy happens later on in the script, not necessarily within the block.

Is there anyway to store %%~i so I can use it later on in the script?


You don't need to store the %%~i variable; it is internal to the FOR command, which is acting on the variable where you stored the directory list. In my code snippet, the :next_step block could happen much farther down the line. As long as the directory list variable %all_dirs% is still good, it should be fine.

Likewise, in aGerman's example, the second and third lines can be separated:

Code: Select all

@echo off &setlocal
set /p "MIG_DEF_DIR=Comma separated directories: "

rem Do a bunch of other stuff...

if defined MIG_DEF_DIR for %%i in (%MIG_DEF_DIR%) do (
  echo "%%~i"
)
pause


Do you understand how FOR loop variables work? I'm not picking on you, they're pretty weird and take some getting used to.

Squashman wrote:Isn't that what Doug's code does already.

Almost, but not quite. The first draft sends the user straight to :next_step if they're done entering directories. This modified version just allows them to continue on, and the next step can happen later. Simms, try running this as it is so you can see what's happening with each directory.

Code: Select all

@echo off &setlocal

:get_dirs
set /p "new_dirs=Comma separated directories: "
set "all_dirs=%all_dirs%, %new_dirs%"

:confirm
echo "Is '%all_dirs%' the full, correct list of directories?"
echo [1] Yes.
echo [2] No, add more.
echo [3] No, start over.

set /p status=
if %status%==1 goto continue
if %status%==2 goto get_dirs
if %status%==3 (set "all_dirs=" & goto get_dirs)
(echo You gave an invalid response.) & goto confirm
:continue

echo Do a bunch of things.
echo Just don't change the value of 'all_dirs'.
echo Much, much later...

:act_on_dirs
rem This bit doesn't actually need a label, since we're not using a GOTO or CALL on it.
rem I'm just keeping it for clarity.
echo The all_dirs variable stores the directory list: %all_dirs%
echo The FOR command breaks down the list and acts on each item in it:
if defined all_dirs for %%i in (%all_dirs%) do (
  echo In this iteration, FOR loop variable "i" is: "%%~i"
  echo Put brackets around it: [%%~i]
  echo Put braces around it: {%%~i}
)

pause
endlocal
exit /b

aGerman
Expert
Posts: 4717
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to store multiple selections from menu prompts?

#11 Post by aGerman » 05 Aug 2016 16:52

Code: Select all

@echo off &setlocal

REM use your set /p algorithm instead ...
set "all_dirs=C:\Users, C:\Windows"

echo(&echo  ********* 1 ***********
if defined all_dirs for %%i in (%all_dirs%) do (
  echo Do stuff with directory "%%~i".
  echo Do something else.
  echo Do other stuff with directory "%%~i".
  echo ~~~~~~~~~~
)

echo(&echo  ********* 2 ***********
if defined all_dirs for %%i in (%all_dirs%) do (
  set "current_dir=%%~i"
  call :proc
)

echo(&echo  ********* 3 ***********
if defined all_dirs for %%i in (%all_dirs%) do (
  echo Do stuff with directory "%%~i".
)
echo ~~~~~~~~~~
echo Do something else.
echo ~~~~~~~~~~
if defined all_dirs for %%i in (%all_dirs%) do (
  echo Do other stuff with directory "%%~i".
)
echo ~~~~~~~~~~

pause
exit /b


:proc
echo Do stuff with directory "%current_dir%".
echo Do something else.
echo Do other stuff with directory "%current_dir%".
echo ~~~~~~~~~~
exit /b

Code: Select all

 ********* 1 ***********
Do stuff with directory "C:\Users".
Do something else.
Do other stuff with directory "C:\Users".
~~~~~~~~~~
Do stuff with directory "C:\Windows".
Do something else.
Do other stuff with directory "C:\Windows".
~~~~~~~~~~

 ********* 2 ***********
Do stuff with directory "C:\Users".
Do something else.
Do other stuff with directory "C:\Users".
~~~~~~~~~~
Do stuff with directory "C:\Windows".
Do something else.
Do other stuff with directory "C:\Windows".
~~~~~~~~~~

 ********* 3 ***********
Do stuff with directory "C:\Users".
Do stuff with directory "C:\Windows".
~~~~~~~~~~
Do something else.
~~~~~~~~~~
Do other stuff with directory "C:\Users".
Do other stuff with directory "C:\Windows".
~~~~~~~~~~
Drücken Sie eine beliebige Taste . . .

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to store multiple selections from menu prompts?

#12 Post by foxidrive » 05 Aug 2016 23:31

SIMMS7400 wrote:A portion of a script I am writing prompts the user to select all directories that are required for their specific process Most times, the user will need to select multiple directories, not just one.

Then, later on the script, those directories the user selected will need to be copied to a another location.


How many directories would the user generally pick from?

This can be done easily with single key entry depending on the situation.
Are all folders being copied to the one location later on?

Post Reply