我使用以下内容获取文件的文件版本:
sigcheck.exe -n "C:\some.exe"
以上返回的内容如下:12,2,0,6837。
如何获取此版本号。删除,制作1个大号&然后将它与另一个版本号进行比较,该版本号存储在txt文件的名称中,例如:12.3.0.7218.txt(即对存储在文件名中的文件版本执行类似的操作:删除。的大小数)。
如果包含其名称中的版本的文件晚于应用程序文件版本(即安装更新),我需要能够在批处理文件中执行一些代码。
答案 0 :(得分:11)
您不能简单地删除逗号(节点分隔符)并比较单个数字 - 它可能无法提供正确的结果。
例如,12.3和1.23都会导致123,导致等价的错误结论。您必须从左侧开始一次比较一个节点。一旦您发现节点的差异,那么您就得到了答案。如果节点是等效的,直到一个版本用完节点,则具有更多节点的版本更大:1.2.2> 1.2
修改强>
有时,公司会将字母后缀添加到节点上。这封信应被视为一个新节点。搜索和替换可用于在每个字母之前引入节点分隔符。搜索和替换不区分大小写,因此a
等于A
。
@echo off
setlocal
call :testVersions 2.1 1.9
call :testVersions "2,1" 1-9
call :testVersions 1.9 2.1
call :testVersions 1.13 1.4
call :testVersions 1.4 1.13
call :testVersions 1.3.4 1.3.4
call :testVersions 1.3.4 1.3.5
call :testVersions 1.3.5 1.3.4
call :testVersions 1.3.5 1.3.5.1
call :testVersions 1.3.5.1 1.3.5
call :testVersions 1.3b 1.3
call :testVersions 1.3 1.3b
call :testVersions 1.10a 1.9
call :testVersions 1.9 1.10a
call :testVersions 1.9a 1.9b
call :testVersions 1.9b 1.9a
call :testVersions 1.9a 1.9A
exit /b
:testVersions version1 version2
call :compareVersions %1 %2
if %errorlevel% == 1 set "result=greater than"
if %errorlevel% == -1 set "result=less than"
if %errorlevel% == 0 set "result=equal to"
echo %~1 is %result% %~2
exit /b
:compareVersions version1 version2
::
:: Compares two version numbers and returns the result in the ERRORLEVEL
::
:: Returns 1 if version1 > version2
:: 0 if version1 = version2
:: -1 if version1 < version2
::
:: The nodes must be delimited by . or , or -
::
:: Nodes are normally strictly numeric, without a 0 prefix. A letter suffix
:: is treated as a separate node
::
setlocal enableDelayedExpansion
set "v1=%~1"
set "v2=%~2"
call :divideLetters v1
call :divideLetters v2
:loop
call :parseNode "%v1%" n1 v1
call :parseNode "%v2%" n2 v2
if %n1% gtr %n2% exit /b 1
if %n1% lss %n2% exit /b -1
if not defined v1 if not defined v2 exit /b 0
if not defined v1 exit /b -1
if not defined v2 exit /b 1
goto :loop
:parseNode version nodeVar remainderVar
for /f "tokens=1* delims=.,-" %%A in ("%~1") do (
set "%~2=%%A"
set "%~3=%%B"
)
exit /b
:divideLetters versionVar
for %%C in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "%~1=!%~1:%%C=.%%C!"
exit /b
- 输出 -
2.1 is greater than 1.9
2,1 is greater than 1-9
1.9 is less than 2.1
1.13 is greater than 1.4
1.4 is less than 1.13
1.3.4 is equal to 1.3.4
1.3.4 is less than 1.3.5
1.3.5 is greater than 1.3.4
1.3.5 is less than 1.3.5.1
1.3.5.1 is greater than 1.3.5
1.3b is greater than 1.3
1.3 is less than 1.3b
1.10a is greater than 1.9
1.9 is less than 1.10a
1.9a is less than 1.9b
1.9b is greater than 1.9a
1.9a is equal to 1.9A
答案 1 :(得分:0)
将逗号转换为[nothing]
set sig=12,2,0,6837
set sig=%sig:,=%
echo sig=%sig%
获取变量的文件名,假设相当多,如名称是目录中最新的 - - 不知道详细信息,抱歉 - 你必须更具体。
set "file="
for /f %%i in ('dir /b /a-d /o-d *.txt') do if not defined file set file=%%~ni
set file=%file:~.=%
echo %file%
但是 - 全部取决于您建议如何检索文件名。
<小时/> 所以如果你使用
:: you are manually entering NEWVER
SET newver=1230718
for /f %%i in ( ' sigcheck.exe -n "C:\some.exe" ' ) do set sig=%%i
Echo As a check, sig retrieved was %sig%
set sig=%sig:,=%
echo Which gets transformed to sig=%sig%
if %sig% lss %newver% (ECHO New version to be installed
) else (echo do not install new version)
现在,如果你想执行
thisbatch 1230718
你可以替换
SET newver=1230718
与
SET "newver=%1"
并且因为1230718
是批处理过程的第一个参数thisbatch
newver
将设置为1230718
答案 2 :(得分:0)
删除逗号:
set "v1=12,2,0,6837"
set "v1=%v1:,=%"
删除点:
set "v2=12.3.0.7218"
set "v2=%v2:.=%"
答案 3 :(得分:0)
在版本号语法与标准.NET版本语法匹配且可以使用PowerShell的情况下,可以使用一种紧凑,清晰的混合.BAT / PowerShell解决方案:
@GOTO :Tests
:CompareVersions
@powershell -command "&{exit(([version]$args[0]).CompareTo([version]$args[1]))}" "%~1" "%~2"
@IF ERRORLEVEL 1 (ECHO "%~1" is newer than "%~2") ELSE (
IF ERRORLEVEL 0 (ECHO "%~1" is the same as "%~2") ELSE ECHO "%~1" is older than "%~2")
@GOTO :eof
:Tests
CALL :CompareVersions 1.2 1.2.0
CALL :CompareVersions 1.2.3 1.2.3.0
CALL :CompareVersions 1.2.3.4 1.2.3.4
CALL :CompareVersions 1.2.3.4 1.2.4.3
CALL :CompareVersions 1.2.4.3 1.2.3.4
CALL :CompareVersions 1.2.3.4 01.2.3.4
CALL :CompareVersions 9.2.3.4 10.2.4.3
CALL :CompareVersions 01.2.3.4 1.2.3.4
CALL :CompareVersions 10.2.4.3 9.2.3.4
CALL :CompareVersions 1.2.3.4 1.2.03.4
CALL :CompareVersions 1.2.9.4 1.2.10.3
CALL :CompareVersions 1.2.03.4 1.2.3.4
CALL :CompareVersions 1.2.10.3 1.2.9.4
前两种情况用来说明标准.NET版本的几个古怪功能-至少需要两个部分,而缺少的部分则为-1。
我不确定OP帖子的更广泛的上下文,但是了解给定.EXE或.DLL,PowerShell可以轻松地从文件中提取IComparable版本(例如{{1} }。