Reputation: 307
Hi i have a script that searches for certain directories based on a string provided by the user, he then presents the directories meeting the user.s inout and offers to cd to them.
@ECHO off
setlocal enableextensions
:start
set scriptname=%0%
set PAUSE_ON_ERROR=yes
rem #
rem ###### SECTION TO CUSTOMIZE #####
rem #
rem #
rem # This is the list of directories where instances of TECSYS iTopia installed under
rem # JBoss are located.
rem #
set dir_to_search_in=C: C:\TecsysDev\iTopiaControlPanel\trunk
rem #
rem ###### END SECTION TO CUSTOMIZE #####
rem #
set argNb=0
for %%x in (%*) do Set /A argNb+=1
if %argNb% GTR 1 (
goto :showUsage
) else if %argNb% == 0 (
set ENV_NAME=*
) else (
set ENV_NAME=*%~1*
)
:MainBlock
set scriptname=%0%
cd /d %~dp0
for /f "usebackq delims=" %%D in (`cd`) do set scriptdir=%%D
for /f "usebackq delims=" %%D in (`cd`) do set CURRENT_DIR=%%D
cd "%scriptdir%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
set JBOSS_DIR=%JBOSS_DIR%\..\..\..\..\..
cd "%JBOSS_DIR%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
cd "%CURRENT_DIR%"
rem Make sure that we have the findstr utility installed.
set findstr_found=
for /f "usebackq" %%f in (`echo x ^| findstr x 2^>nul`) do set findstr_found=%%f
if "X%findstr_found%" == "X" (
echo The findstr utility is not found.
goto pauseforError
)
call :getJbossVersion
rem prepare the list of special JBoss environments to exclude
if /i "%JBOSS_VERSION%" lss "5" (
set env_to_exclude=all default minimal
) else if /i "%JBOSS_VERSION%" lss "6" (
set env_to_exclude=all default minimal standard web
) else (
set env_to_exclude=all default minimal jbossweb-standalone standard
)
rem find the environment directories
for %%f in (%dir_to_search_in%) do (
for /f "delims=" %%G in ('dir /b /ad "%%~f\jboss*"') do (
if exist "%%f\%%G\server\%ENV_NAME%" (
for /f "delims=" %%H in ('dir /b /ad "%%~f\%%~G\server\%ENV_NAME%"') do (
call :concat %%~f\%%~G\server\%%~H
)
)
)
)
if "X%environment_home_list%" == "X" (
echo Invalid %1 environment requested.
goto :pauseforError
)
rem display the folders
setlocal enabledelayedexpansion
rem checking if a unique instance of the environment exists
Set Count=0
for %%t in (%environment_home_list%) do (
Set /A Count+=1
)
if %count% == 1 (
for /f "tokens=1 delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
goto :cdToEnv
)
)
:chooseEnvironment
Set Count=1
echo.
echo Please choose an environment:
echo.
echo 0^) Exit
for %%z in (%env_home_list%) do (
echo !Count!^) %%z
Set /A Count+=1
)
rem reading user input
SET /P userChoice=
set /a Test=userChoice
if !Test! EQU 0 (
if not !userChoice! EQU 0 (
echo Please enter a valid number
goto :chooseEnvironment
)
)
rem checking if the user wants to exit
if %userChoice% EQU 0 (
goto :END
)
Set /A Count-=1
rem making sure the input is valid
if %userChoice% LSS 1 (
echo Please enter a valid choice
goto :chooseEnvironment
)
if %userChoice% GTR %count% (
echo Please enter a valid choice
goto :chooseEnvironment
)
rem matching the user input with the right element from the environment list
for /f "tokens=%userChoice% delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
)
:cdToEnv
rem taking the environment name to change the prompt
for /f "usebackq delims=" %%D in (`dir /b %chosenEnv%*`) do set chosenEnvName=%%D
rem setting the right path to cd to.
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.ear
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.war
)
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%
)
set prompt=%chosenEnvName%^>
cmd /k cd /d %chosenEnvHome%
goto :End
:concat
rem defining our list of apps installed excluding special jboss folders
set is_env_to_exclude=
for %%L in (%env_to_exclude%) do (
for /f "usebackq delims=" %%U in (`echo %1 ^| findstr %%L`) do (
set is_env_to_exclude=yes
)
)
if "X%is_env_to_exclude%" == "X" (
set environment_home_list=%1 %environment_home_list%
set env_home_list=%~n1 %env_home_list%
)
goto :eof
:getJbossVersion
for /f "usebackq tokens=2" %%v in (`echo. ^| %JBOSS_DIR%\bin\run.bat -V ^| ^
findstr "JBoss" ^| findstr /i /v "BootStrap"`) do (
set JBOSS_VERSION=%%v
)
goto :EOF
:showUsage
echo Usage: tish [environment]
echo ^|
echo +--- installed environment
:pauseforError
if "%PAUSE_ON_ERROR%" == "yes" pause
:End
My only problem is that i want the folders placed in env_home_list to be displayed alphabatically, icannot do the sort directly in the dir command, because i want to order alphabetically all the folder found and not group them by their parent folder. And i insist that i am not using a file here but only a list.
Upvotes: 2
Views: 3898
Reputation: 67216
You may use the fact that Batch environment variables are automatically keept in sorted order by their names, so you need to insert the value of the variable inside the name of the variable. I suggest you to do that using the standard array naming convention and insert the value as subscript, inside the brackets:
@echo off
setlocal EnableDelayedExpansion
set list=b c a
rem Separate list elements into an array, use X as a dummy value for array elements
for %%a in (%list%) do (
set elem[%%a]=X
)
rem Process the elements in sorted order:
for /F "tokens=2 delims=[]" %%a in ('set elem[') do echo %%a
Note that this method eliminate duplicated elements and keeps just the last value. If this is a problem, you need to also insert a counter after the value (inside the brackets).
Upvotes: 8