如何根据文件夹名称识别最新的应用程序构建文件夹
How to identify latest application build folder based on folder name
我正在努力寻找一种智能方法来确定最新的构建文件夹,该文件夹位于许多其他文件夹中,我希望有人能为我指明正确的方向。
主应用程序安装路径:
C:\程序Files\ABC
子文件夹:
- 杂项文件夹01
- 杂项文件夹02
- 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" 是最新的,我需要设置 "App Name 5.0.2.0"到变量 %LATEST_BUILD%
我希望这对大家有意义。非常感谢任何帮助。
谢谢
一个简单的方法是使用 FOR
循环和 dir /b
来获取所有文件夹名称。从那里我们可以使用基本的 greater 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 /?
- 等等。
我可能会使用以下代码解决此任务(请参阅所有解释性 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
),每个版本号最多可以包含两位小数。
我正在努力寻找一种智能方法来确定最新的构建文件夹,该文件夹位于许多其他文件夹中,我希望有人能为我指明正确的方向。
主应用程序安装路径: C:\程序Files\ABC
子文件夹:
- 杂项文件夹01
- 杂项文件夹02
- 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" 是最新的,我需要设置 "App Name 5.0.2.0"到变量 %LATEST_BUILD%
我希望这对大家有意义。非常感谢任何帮助。
谢谢
一个简单的方法是使用 FOR
循环和 dir /b
来获取所有文件夹名称。从那里我们可以使用基本的 greater 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 /?
- 等等。
我可能会使用以下代码解决此任务(请参阅所有解释性 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
),每个版本号最多可以包含两位小数。