I want to make a batch file that will search for all files in a directory that are .sln files and then display them as a list, so the user can select which one they want from the list and it will open that file. Not sure about how to store the file to make them into a list. This is what i have so far..
set TT_API_PATH=C:\dir
cd %TT_API_PATH%
dir /b /s *.sln
set cmd=dir /b /s *.sln
FOR /F %%i IN (' %cmd% ') DO SET X=%%i
EDIT: Replaced the unnecessary and dangerous
call echo %%x] !choice[%%x]!command withecho %%x] !choice[%%x]!. Not even sure why I included it in the first place.NOTE:
Ignore the lone
:lines, for some reason all the blank lines in my code collapsed, and it was nessicary to put something on blank lines to keep the formatting. Sorry.EDIT: Changed example to include subdirectories as requested.
To meet your request for sln files found in subdirectories, I changed the
for %%x in (*.sln), which returns only the sln files found in the current directory, tofor /f %%x in ('dir /s /b *.sln')which executes thedir /s /b *.slncommand in the current directory and returns the results.To make the output as clean and clear as possible, I also changed what was printed when listing the files from the entire path
!choice[%%x]!, to the relative path starting from the current directory!choice[%%x]:%cd%\=!( Which replaces the current path and directory (%cd%) plus a backslash (\) with nothing).The above code displays only the relative path from the current directory to the files, but leaves the full path inside the variable
choice[#].If you don’t want to deal with the entire path at all, you can add a single line to the
for /f %%x...loop, and restore the echo line in thefor /l %%x ...loop to that in the (edited) first example, so that the code looks like this (Explaining where the changes go takes just as long as showing the entire code, so):Editing / restoring that
echostatement is not strictly necessary, and does not change the output at all, but it’s good programming practice because it just recognizes the fact that the path of the current directory in thechoice[#]variable was already removed when the filenames were originally read in. Changing it removes unnecessary complexity and removes possible unexpected results if the code is ever changed or adapted for some other use. If you leave the line alone, it will once again attempt to replace all instances of the current directory in the variablechoice[#]with nothing. When it fails to replace anything it will simply leave the string unchanged.