2013-12-11 116 views
2

嗨,我有一個腳本,基於用戶提供的字符串搜索某些目錄,然後他提出目錄滿足user.s inout並提供cd給他們。如何對批次中的字符串列表進行排序?

@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 

我唯一的問題是,我想成爲alphabatically顯示放置在env_home_list的文件夾,icannot直接做排序的dir命令,是因爲我想通過以下順序按字母所有的文件夾中,而不是將它們分組的父文件夾。我堅持認爲我沒有在這裏使用一個文件,而只是一個列表。

+2

您的代碼很難閱讀。你能不能把這個故事有所改變。 – captcha

+0

好吧,讓我們說我有這個列表列表= b c d,我想排序它,並diplay a b c,我該怎麼做? – user2966439

回答

6

您可以使用一個事實,即批處理環境變量在排序順序由他們的名字自動keept,所以你需要插入名義變量的變量的值。我建議你做的是使用標準的陣列命名約定,並插入值作爲下標,括號內:

@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 

注意,此方法消除重複元素,並只保留最後一個值。如果這是一個問題,您還需要在值後面插入一個計數器(在括號內)。

+0

非常好謝謝+1 – SachaDee

+0

總是一樣的,我喜歡它:T。 – Endoro

+0

謝謝你。我會嘗試。 – user2966439

相關問題