批处理以从行数中找到最高版本的字符串 [英] Batch to find highest version-string from amount of lines
问题描述
我需要确定批处理脚本中LocalDB的最高安装版本,然后将整个版本字符串设置为变量.
I need to determine what's the highest installed version of LocalDB within a batch script and set the whole version string into a variable.
sqllocaldb版本
产生如下输出:
Microsoft SQL Server 2012 (11.0.5058.0)
Microsoft SQL Server 2014 (12.0.2000.8)
版本字符串的顺序不一定是升序.需要一些帮助将最高服务器版本设置为变量(在本示例中为12.0.2000.8).
The order of the version string is not necessarily ascending. Need some help to set the highest server version into a variable (in this example 12.0.2000.8).
推荐答案
您需要调用命令并结合使用 FOR
命令和 SET
命令
you need to invoke the command and parse the returned string, with a combination of FOR
command and SET
command,
for /f "tokens=5" %%a in ('sqllocaldb versions') do (
set verstr=%%a
set verstr=!verstr:^(=!
set verstr=!verstr:^)=!
然后使用另一个 FOR
命令将版本字符串解析为片段,并使用 SET/A
和简单的算术
then parse the version string into pieces with another FOR
command, and compute a version number with a SET /A
and simple arithmetic
for /f "tokens=1,2,3 delims=." %%b in ("!verstr!") do (
set /a verval=%%b*1000000+%%c*1000+%%d
然后使用 IF GTR
命令
if !verval! gtr !highval! (
set /a highval=!verval!
set highest=!verstr!
)
因此,将所有部件放在一起....
so, putting all the pieces together....
@echo off
setlocal enabledelayedexpansion
set /a highval=0
for /f "tokens=5" %%a in ('sqllocaldb versions') do (
set verstr=%%a
set verstr=!verstr:^(=!
set verstr=!verstr:^)=!
for /f "tokens=1,2,3 delims=." %%b in ("!verstr!") do (
set /a verval=%%b*1000000+%%c*1000+%%d
if !verval! gtr !highval! (
set /a highval=!verval!
set highest=!verstr!
)
)
)
echo !highest!
这篇关于批处理以从行数中找到最高版本的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!