如何批量排序字符串列表?

时间:2013-12-11 16:37:25

标签: string sorting batch-file

您好我有一个脚本,根据用户提供的字符串搜索某些目录,然后他会显示符合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

我唯一的问题是我希望按字母顺序显示放在env_home_list中的文件夹,不要直接在dir命令中进行排序,因为我想按字母顺序排序所有找到的文件夹,而不是按父文件夹对它们进行分组。我坚持认为我不是在这里使用文件而只是列表。

1 个答案:

答案 0 :(得分:7)

您可以使用批处理环境变量按其名称按顺序自动保存的事实,因此您需要在名称中插入变量的值变量。我建议你使用标准的数组命名约定,并在括号内插入值作为下标:

@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

请注意,此方法可消除重复元素并仅保留最后一个值。如果这是一个问题,您还需要在值之后插入一个计数器(在括号内)。