如何根据文件夹名称识别最新的应用程序构建文件夹

时间:2018-12-11 02:20:43

标签: windows batch-file cmd windows-7

我正在努力寻找一种明智的方法来确定许多其他文件夹中的最新构建文件夹,希望我能指出正确的方向。

主应用程序安装路径: C:\ Program Files \ ABC

子文件夹:

  • MiscFolder01
  • MiscFolder02
  • AppName 5.0
  • AppName 5.0.0.0
  • AppName 5.0.1.0
  • AppName 5.0.2.0

用户可以安装“ AppName 5. *”文件夹中的任何一个。如果在安装更高版本之前未正确卸载旧版本,则可能还会有“ AppName 5. *”文件夹的任意组合。

因此,我需要根据文件夹名称确定安装的最新版本,并将其设置为变量“ LATEST_BUILD”。

示例1:用户安装了“ App Name 5.0”,“ App Name 5.0.0.0”和“ App Name 5.0.2.0”,但是由于“ App Name 5.0.2.0”是最新的,因此我需要设置“应用名称5.0.2.0“转换为变量%LATEST_BUILD%

我希望这对大家都有意义。任何帮助将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:2)

一种简单的方法是使用FOR循环和dir /b来获取所有文件夹名称。从那里,我们可以使用基本的then语句来比较 BuildFolderName 之后的所有数字。

Main.Bat

@ECHO OFF
@setlocal enabledelayedexpansion

Rem | Configuration (Directory Path) & (BuildFolderName)
set "InstallPath=C:\Program Files\ABC"
set "BuildFolderName=AppName"
CD %InstallPath%

Rem | Get Folder "AppName*" to String
set "Latest=0"
for /f "tokens=*" %%A in ('dir /b^| find /I "%BuildFolderName%"') do (

    Rem | Extract Numbers From String
    for /f "tokens=1,*" %%B in ('Echo %%A') do (

        Rem | Find Largest Number
        Set "NUM=%%C"
        if !NUM! GTR !MAX! set "Latest=%BuildFolderName% !NUM!"
    )
)

Rem | Here Is The Latest (Largest) File
Echo Your Latest Update Is: %Latest%

pause
goto :EOF

要获取有关任何命令的帮助,请执行以下操作:

  • call /?
  • set /?
  • for /?
  • if /?
  • find /?
  • 等等。

答案 1 :(得分:1)

我可能会使用以下代码解决此任务(请参见所有解释性的rem备注):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=C:\Program Files\ABC" & rem // (root directory)
set "_NAME=AppName"              & rem // (folder name prefix)

rem // Initialise variables:
set /A "HIGHEST=0" & set "LATEST="
rem // Loop through potentionally matching folders:
for /F "delims=| eol=|" %%F in ('dir /B /A:D "%_ROOT%\%_NAME% *"') do (
    rem // Split off last space-separated name portion:
    set "FOLDER=%%F" & set "NUMBER="
    setlocal EnableDelayedExpansion
    for %%H in ("!FOLDER: =" "!") do (if not defined NUMBER endlocal) & set "NUMBER=%%~H"
    rem // Filter for name portions consisting of numerals and dots only:
    setlocal EnableDelayedExpansion
    for /F "tokens=* eol= " %%E in ('echo("!NUMBER!" ^| findstr "\"[0-9\.]*\""') do (
        endlocal
        rem // Extract individual version numbers (four at most):
        for /F "tokens=1-4 delims=. eol=." %%A in ("%%E.0.0.0.0") do (
            rem /* Compute a number out of the individual version numbers;
            rem    note that none of them must have more than two digits: */
            setlocal EnableDelayedExpansion
            set /A "CURRENT=((%%A*100+%%B)*100+%%C)*100+%%D"
            rem // Compare the current number with the stored greatest one:
            if !CURRENT! gtr !HIGHEST! (
                rem // Store the current number as the greatest one in case it is such:
                for /F %%H in ("!CURRENT!") do endlocal & set /A "HIGHEST=%%H"
                rem // Store the folder name correlating with the current number:
                set "LATEST=%%F"
            ) else endlocal
        )
    )
)
rem // Return the resulting folder name:
echo Found folder name: %LATEST%
echo Latest version:    %NUMBER%
echo Comparison number: %HIGHEST%

endlocal
exit /B

这可以处理最多四个单独的版本号(例如5.0.2.0)的文件夹名称,每个版本号最多可以包含两个十进制数字。