Page 1 of 1
How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 12:19
by SIMMS7400
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!
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 13:04
by aGerman
The only selection I see is either 1 (for Yes) or 2 (for No). What are you talking about?
Regards
aGerman
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 13:46
by SIMMS7400
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.
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 14:02
by aGerman
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
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 14:32
by SIMMS7400
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!
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 15:51
by douglas.swehla
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
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 16:12
by SIMMS7400
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?
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 16:32
by Squashman
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.
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 16:35
by SIMMS7400
Ah yes, testing now!
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 16:40
by douglas.swehla
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
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 16:52
by aGerman
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
Re: How to store multiple selections from menu prompts?
Posted: 05 Aug 2016 23:31
by foxidrive
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?