Arvind Chourasiya
Arvind Chourasiya

Reputation: 17402

How to increment value of int variable of given file by 1 batch script

I need to increment value of int variable versionCode by 1 every time I run batch script.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.sisapp.in.globalthesc" android:versionName="2.5" android:versionCode="8">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="28" />
    <supports-screens android:resizeable="true"  android:largeScreens="true"/>
    <application android:icon="@drawable/sisIconLaunch">        
        <receiver android:name=".DeviceBootReceiver" />
    </application>
</manifest>

This code I am using, it making back versionCode to 1 and if 1 than nothing increasing.

@echo off
setlocal enabledelayedexpansion
set "inputfile=D:\Arvind.ch\SIS\SIS_Product\SIS-Global-Dev\edTheSIS\AppIcons\Global\MenifestFile\AndroidManifest.xml"

    for /f tokens^=10^delims^=^" %%i in ('type "%inputfile%" ^| findstr "android:versionCode"') do set vers=%%i
    for /f "tokens=1,2 delims=." %%i in ("!vers!") do (
         set decia=%%i

        set /a decia+=1    
        set newver=!decia!
    )
    for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
         set "str=%%a"
         call set "str=%%str:*]=%%"
         if "!str:~0,15!" == "<manifest xmlns" set "str=!str:%vers%=%newver%!"
         >>%inputfile% echo(!str!
  )

Upvotes: 0

Views: 104

Answers (2)

user7818749
user7818749

Reputation:

As explicitely stipulated in the previous question batch is not the best tool for this and if the xml format changes by any means especially on the <manifest line it will not work. So regardless, here is a combined script (previous question) and this one. Give it a try and let me know.

@echo off
setlocal enabledelayedexpansion
set "inputfile=D:\Arvind.ch\SIS\SIS_Product\SIS-Global-Dev\edTheSIS\AppIcons\Global\MenifestFile\AndroidManifest.xml"

for /f tokens^=8^,10delims^=^" %%i in ('type "%inputfile%" ^| findstr "android:versionName"') do (
    set vers=%%i
    set code=%%j
)
for /f "tokens=1,2 delims=." %%i in ("!vers!") do (
     set decia=%%i
     set decib=%%j
     if "!decib!" lss "9" (
            set /a decib+=1
      ) else (
            set decib=0
            set /a decia+=1
    )
    set newver=!decia!.!decib!
)
for /f "tokens=1" %%i in ("!code!") do (
     set cnt=%%i
     set /a cnt+=1
)
set newcode=!cnt!
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
     set "str=%%a"
     call set "str=%%str:*]=%%"
     if "!str:~0,15!" == "<manifest xmlns" set "str=!str:"%vers%"="%newver%"!"
     if "!str:~0,15!" == "<manifest xmlns" set "str=!str:"%code%"="%newcode%"!"
     >>%inputfile% echo(!str!
)

The reasons why you had issues were because you mixed up variable names from the old code and the new. This will set a value from the one to the next, also, because we are running the same searches throughout the script, old stuff will update with new, so again, this is not the best solution, powershell would have been a better one. But I included some double quotes in the replacement to try and be more specific, but it is not perfect.

Upvotes: 1

user6811411
user6811411

Reputation:

As you were told in your previous question, better handle xml files with xml tools.

Batch isn't suited well for this task. OTOH PowerShell is and can be used as a tool from batch and has no issues with UTF8 encoding.

Powershell

$File = 'D:\Arvind.ch\SIS\SIS_Product\SIS-Global-Dev\edTheSIS\AppIcons\Global\MenifestFile\AndroidManifest.xml'
$xml = New-Object XML
$xml.Load($File)
$xml.manifest.Versioncode = [string]([int]$xml.manifest.Versioncode +1)
$xml.manifest.VersionName = [string]([double]$xml.manifest.VersionName +.1)
$xml.Save($File)

To be on topic wrapped in batch:

@echo off
set "inputfile=D:\Arvind.ch\SIS\SIS_Product\SIS-Global-Dev\edTheSIS\AppIcons\Global\MenifestFile\AndroidManifest.xml"
powershell -NoP -NoLogo -C "$File='%inputfile%';$xml = New-Object XML;$xml.Load($File);$xml.manifest.Versioncode = [string]([int]$xml.manifest.Versioncode +1);$xml.manifest.VersionName = [string]([double]$xml.manifest.VersionName +.1);$xml.Save($File)"

Another less desirable solution is to use a RegEx (here with lookarounds) PowerShell

$File = 'D:\Arvind.ch\SIS\SIS_Product\SIS-Global-Dev\edTheSIS\AppIcons\Global\MenifestFile\AndroidManifest.xml'
$RE1 = '(?<=android:versionCode=")\d+(?=")'
$RE2 = '(?<=android:versionName=")[\d.]+(?=")'
$NewCode =    [int](Select-String -Path $File -Pattern $RE1 ).Matches.Value +1
$NewName = [double](Select-String -Path $File -Pattern $RE2 ).Matches.Value +.1
(Get-Content $File -Encoding UTF8 -Raw) -Replace $RE1,$NewCode -Replace $RE2,$NewName| 
 Set-Content $File -Encoding UTF8

It may similarly be wrapped in a batch-file.

Upvotes: 2

Related Questions