I have searched and searched to find a solution to (what feels like) a unique problem. Many answers here have been quite helpful and have gotten me a long way, but the last bit has me stumped.
- My batch file needs to open an executable in a folder with a variable name
- This folder may be in one of two directories
This is what I originally had and it works
@ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET DIR1="%USERPROFILE%\AppData\Local\Application Workspace\Profiles" SET DIR2=C:\Application\SRW\Profiles IF NOT EXIST %DIR1% ( GOTO PROFILE_2 ) ELSE ( GOTO PROFILE_1 ) :PROFILE_1 for /f "delims=" %%A in ('dir %DIR1% /ad /b') do ( set foldername=%%~nxA ) SET DIR1=%DIR1:"=% SET DIR=%DIR1%\%foldername%\app.exe GOTO START_APP :PROFILE_2 for /f "delims=" %%A in ('dir %DIR2% /ad /b') do ( set foldername=%%~nxA ) SET DIR=%DIR2%\%foldername%\app.exe GOTO START_APP :START_APP START "" /b "%DIR%" :EOF ENDLOCAL EXIT
Then I was thrown a curveball when I discovered that some users may have multiple profiles in the variable profile folder and they need to be able to select which one to use for that given task. Now I have a variable profile folder and either one or multiple variable profiles within that folder.
I have found code to list the folder names and display them in the command window for selection.
@echo off
cls
setlocal EnableDelayedExpansion
set /a count=0
for /d %%d in (*) do (
set /a count+=1
@echo !count!. %%d
)
setlocal DisableDelayedExpansion
set /P selection="select folder number:"
I also found this routine which allows the user to select a file from a list then it is supposed to translate that file name into a variable.
Unfortunately, I cannot get the example in the link to work as is and I have no idea how to make it work with folder names as the folder name example and the file name example are close but not close enough for me to understand what to do. And even if it somehow does manage to work, how then can I make such a routine work within the original code posted above?
In addition, I really don't want the user to be forced to make a folder selection if there is only one folder. If only one folder exists, it should be placed into the folder name variable automatically and nothing displays to the user at all.
Is what I'm trying to do even possible at all?
Any help is most appreciated.