我的文件中有一个汇编版本
AssemblyInfo.cs
由我手动更新。1.0.0.512
有没有办法让版本号加1?
我可以成功自动更新文件,但需要增加版本号。
我尝试了以下方法:
@set a="1.0.0.512"
@set /a "c=%a%+1"
echo %c%
但是它会在命令行上导致以下错误
@set /a "c=%a%+1"
:
缺少操作员。
我希望只有最后一个数字会递增,生成版本字符串上方的示例
1.0.0.513
。
第一个错误是在第一个命令行
@set a="1.0.0.512"
上,它用字符串a
定义了环境变量"1.0.0.512"
(无意义的变量名)。是的,双引号也被分配给环境变量。正确的、更好的语法是 @set "a=1.0.0.512"
。请参阅我的答案 Why is no string output with 'echo %var%' after using 'set var = text' on command line? 解释关于第一个 "
在命令行上使用命令 放置的位置的巨大差异设置。
第二个错误是没有将版本号拆分为四个 32 位有符号整数值以仅增加最后一个整数值,然后再次将四个整数连接到版本字符串。
1.0.0.512
既不是有效的整数,也不是有效的浮点数。因此,没有一种编程或脚本语言支持仅通过一个命令来递增此版本字符串的最后一个数字。
下面的批处理代码可用于以下四种格式之一的版本字符串:
1
、2
、...1.2
,2.7
,...3.1.47
、8.2.3648
、...1.3.0.39
、2.7.1.39024
、...增量是版本字符串的最后一个数字。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FullVersion=%~1"
if not defined FullVersion set "FullVersion=1.0.0.512"
echo Version to increment is: %FullVersion%
rem Make sure that the environment variables used below are
rem not defined already by chance outside the batch file.
set "MajorVersion="
set "MinorVersion="
set "Maintenance="
set "BuildNumber="
rem Split up the version string into four integer values.
for /F "tokens=1-4 delims=." %%I in ("%FullVersion%") do (
if not "%%L" == "" (
set "MajorVersion=%%I."
set "MinorVersion=%%J."
set "Maintenance=%%K."
set "BuildNumber=%%L"
) else if not "%%K" == "" (
set "MajorVersion=%%I."
set "MinorVersion=%%J."
set "BuildNumber=%%K"
) else if not "%%J" == "" (
set "MajorVersion=%%I."
set "BuildNumber=%%J"
) else set "BuildNumber=%%I"
)
rem Remove leading zeros from build number to get the build number always
rem interpreted as decimal number and never as valid or invalid octal number.
if defined BuildNumber for /F "tokens=* delims=0" %%I in ("%BuildNumber%") do set "BuildNumber=%%I"
rem It is possible that the build number is not defined anymore because of
rem having value 0. But that is no problem on using the arithmetic expression
rem below which uses value 0 for not defined environment variable BuildNumber
rem and so BuildNumber has the correct value 1 after the next command line.
set /A BuildNumber+=1
rem The command lines below can be enabled by removing REM to define
rem the build number with a fixed number of digits like four digits.
REM set "BuildNumber=000%BuildNumber%"
REM set "BuildNumber=%BuildNumber:~-4%"
rem Concatenate the version string together with incremented build number.
set "FullVersion=%MajorVersion%%MinorVersion%%Maintenance%%BuildNumber%"
echo Incremented version is: %FullVersion%
endlocal
要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。
echo /?
endlocal /?
for /?
if /?
rem /?
set /?
setlocal /?
我写这个更简单的解决方案只是为了好玩......
@echo off
setlocal EnableDelayedExpansion
rem Read the assembly version
set /P "version=" < AssemblyInfo.cs
rem Increment the last number
set "i=1" & set "v1=%version:.=" & set /A i+=1 & set /A "v!i!=%+1"
rem Update the file
> AssemblyInfo.cs echo %v1%.%v2%.%v3%.%v4%
如果您想知道此方法是如何工作的,请删除
@echo off
命令并查看执行的代码... ;)