Solved How i can replace value of file.xml with file.bat?

February 16, 2018 at 03:09:59
Specs: Windows 10
my question is:

i want write one value beetween a tag xml, but i don't know how i find it and replace

this is my xml

and tag's value that i want replace is <BuildNumber>**This**</Buildnumber>

i have try more batch file, but all delete the text

<Target Name="SetVersionValues" Outputs="" Condition="" DependsOnTargets="">
<!-- The regular expression ".+(?=\d{4})" means "find one or more character until the sub expression 'four consecutive digits' is matched to the right" -->
<!-- By finding everything before the 4 digits, instead of looking for "everything up to 'digit' dot 'digit' and then 'underscore' we gain more flexibility when using CruiseControl labels to do BC builds -->
<!-- This works correctly for the following style of labels from CruiseControl: GMS_1.1_BC_0058, Pawlowski_TestBuilds_0224, and GMS_1.1_0763 -->
<RegexReplace Input="$(CCNetLabel)" Expression=".+(?=\d{4})" Replacement="" Count="">
<Output ItemName="CCNetLabelRegexed" TaskParameter="Output" />
</RegexReplace>
<PropertyGroup>
<BuildNumber>0001</BuildNumber>
</PropertyGroup>
<PropertyGroup>
<ISMVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ISMVersion>
<ISMVersionST>$(MajorVersion).$(MinorVersion).$(BuildNumber)</ISMVersionST>
<ProductVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ProductVersion>
</PropertyGroup>
</Target>


See More: How i can replace value of file.xml with file.bat?

Report •

#1
February 16, 2018 at 03:14:56
✔ Best Answer
with what do you want to replace:
<BuildNumber>
</Buildnumber>

Please explain what string needs to be inserted instead of BuildNumber
or do you want to remove the entire line?

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450


Report •

#2
February 16, 2018 at 03:53:56
this will replce what you want, but i do not know what to replace it with

::----start of test.bat----
@echo off
set "file=test.xml"
for /f "tokens=1 delims=[]" %%a in ('type "%file%" ^| find /N "<BuildNumber>"') do set linenumber=%%a
for /f "tokens=1,2* delims=[]" %%a in ('type "%file%" ^| find /N /v ""') do if %%a LSS %linenumber% echo %%b>>output.xml
for /f "tokens=1-3 delims=<>" %%a in ('type "%file%" ^| findstr "<BuildNumber>"') do echo ^<this^>keep this:%%b?^</that^> >>output.xml
for /f "tokens=1,2* delims=[]" %%a in ('type "%file%" ^| find /N /v ""') do if %%a GTR %linenumber% echo %%b>>output.xml
::----end of test.bat----


output.xml contains:

<Target Name="SetVersionValues" Outputs="" Condition="" DependsOnTargets="">
<-- The regular expression ".+(?=\d{4})" means "find one or more character until the sub expression 'four consecutive digits' is matched to the right" -->
<-- By finding everything before the 4 digits, instead of looking for "everything up to 'digit' dot 'digit' and then 'underscore' we gain more flexibility when using CruiseControl labels to do BC builds -->
< GMS_1.1_BC_0058, Pawlowski_TestBuilds_0224, and GMS_1.1_0763 -->
<RegexReplace Input="$(CCNetLabel)" Expression=".+(?=\d{4})" Replacement="" Count="">
<Output ItemName="CCNetLabelRegexed" TaskParameter="Output" />
</RegexReplace>
<PropertyGroup>
<this>keep this:0001?</that>
</PropertyGroup>
<PropertyGroup>
<ISMVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ISMVersion>
<ISMVersionST>$(MajorVersion).$(MinorVersion).$(BuildNumber)</ISMVersionST>
<ProductVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ProductVersion>
</PropertyGroup>
</Target>

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m BiosMod
VS450

message edited by hidde663


Report •

#3
February 24, 2018 at 00:30:42
i want replace with an INT

<BuildingNumber> 0000 <\BuildingNumber>
after batch file
<BuildingNumber> 0001 <\BuildingNumber>


Report •

Related Solutions

#4
February 24, 2018 at 06:01:08
replace this line:

for /f "tokens=1-3 delims=<>" %%a in ('type "%file%" ^| findstr "<BuildNumber>"') do echo ^<this^>keep this:%%b?^</that^> >>output.xml

with

for /f "tokens=1-3 delims=<>" %%a in ('type "%file%" ^| findstr "<BuildNumber>"') do echo ^<BuildNumber^>0001?^</BuildNumber^> >>output.xml

output.xml contains:

<Target Name="SetVersionValues" Outputs="" Condition="" DependsOnTargets="">
<-- The regular expression ".+(?=\d{4})" means "find one or more character until the sub expression 'four consecutive digits' is matched to the right" -->
<-- By finding everything before the 4 digits, instead of looking for "everything up to 'digit' dot 'digit' and then 'underscore' we gain more flexibility when using CruiseControl labels to do BC builds -->
< GMS_1.1_BC_0058, Pawlowski_TestBuilds_0224, and GMS_1.1_0763 -->
<RegexReplace Input="$(CCNetLabel)" Expression=".+(?=\d{4})" Replacement="" Count="">
<Output ItemName="CCNetLabelRegexed" TaskParameter="Output" />
</RegexReplace>
<PropertyGroup>
<BuildNumber>0001</BuildNumber>
</PropertyGroup>
<PropertyGroup>
<ISMVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ISMVersion>
<ISMVersionST>$(MajorVersion).$(MinorVersion).$(BuildNumber)</ISMVersionST>
<ProductVersion>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</ProductVersion>
</PropertyGroup>
</Target>

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1340c/1965m BiosMod
VS450

message edited by hidde663


Report •

#5
March 5, 2018 at 05:18:00
file bat write a new row, but the row that i want replace is in down the file, the original row don't change

Report •

#6
March 6, 2018 at 01:27:50
this will do as you asked, overwriting test.xml but will first make a backup of test.xml & save it as "test - Backup.xml"

::----start of test.bat----
@echo off
set "file=test.xml"
for /f "tokens=1 delims=[]" %%a in ('type "%file%" ^| find /N "<BuildNumber>"') do set linenumber=%%a
for /f "tokens=1,2* delims=[]" %%a in ('type "%file%" ^| find /N /v ""') do if %%a LSS %linenumber% echo %%b>>output.xml
for /f "tokens=1-3 delims=<>" %%a in ('type "%file%" ^| findstr "<BuildNumber>"') do echo ^<BuildNumber^>0001^</BuildNumber^> >>output.xml
for /f "tokens=1,2* delims=[]" %%a in ('type "%file%" ^| find /N /v ""') do if %%a GTR %linenumber% echo %%b>>output.xml
ren "%file%" "%file:~-4% - Backup.xml"
ren "output.xml" "%file%"
exit/b
::----end of tets.bat----

i5-6600K[delid]@4.814GHz/4.613GHz cache@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2808MHz@1.37v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1340c/1965m BiosMod
VS450

message edited by hidde663


Report •

#7
March 30, 2018 at 02:49:50
Sorry for time request, i want repalce with a number

example, i run bat and write next number

before
0001
after
0002
again...
0003
and more


Report •

#8
March 30, 2018 at 10:24:01
this will increase the buildnumber by 1 located in test.xml

I did notice a problem with echoing the file to another, the code on lines 2,3 & for have a missing ! sign, aswell as line 3 missing an entire sentence.

working on a solution, but may take a few days since i do not have much time atm.

::----start of test.bat----
@echo off&setlocal enabledelayedexpansion
set "file=test.xml"
for /f "tokens=1 delims=[]" %%a in ('type "%file%" ^| find /N "<BuildNumber>"') do set linenumber=%%a
for /f "tokens=1,2* delims=[]" %%a in ('type "test.xml" ^| find /N /v ""') do if %%a LSS 5 echo %%b>>output.xml
for /f "tokens=2 delims=<>" %%a in ('type "%file%" ^| findstr "<BuildNumber>"') do (
set "string=%%a"
if "!string:~0,1!"=="0" set "string=!string:~1!"
if "!string:~0,1!"=="0" set "string=!string:~1!"
if "!string:~0,1!"=="0" set "string=!string:~1!"
set/a string+=1
SET string=0000!string!
SET string=!string:~-4!
echo ^<BuildNumber^>!string!!^</BuildNumber^> >>output.xml
)
for /f "tokens=1,2* delims=[]" %%a in ('type "%file%" ^| find /N /v ""') do if %%a GTR !linenumber! echo %%b >>output.xml
ren "!file!" "!file:~0,-4! - Backup.xml"
del "!file!"
ren "output.xml" "!file!"
exit/b
::----end of test.bat----

i5-6600K[delid]@4.8GHz/4.5GHz@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-30 1T 2800MHz@1.36v
ASUS Z170K | Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1355/1940 BiosMod

message edited by hidde663


Report •

#9
March 30, 2018 at 12:45:09
A few comments:

1) Batch script is the worst possible language for XML handling, because it makes no distinction between data and commands, and both "<" and ">" are used by batch. If you're using a Windows environment, I'd recommend using VBScript or PowerShell. Both of which can parse XML.

2) alejd34184: $(BuildNumber)
Is this set previously by the XML, or could you just:

<BuildNumber>$(BuildNumber)</BuildNumber>
And call it a day?

hidde663:
3) Your entire string length handling can be replaced with:

SET string=0000%string%
SET string=%string:~-4%

4) Unless I'm missing something, your script isn't handling the octal issue, so expect problems once you try moving past 0008.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#10
March 30, 2018 at 13:09:32
it would if it where using the :strlen subroutine

nvm, you're right, just tested it to confirm

edit:fixed the "8" issue, still have the line 2,3,4 issue tho
the problem is the exclamation mark

example lines 2-4:
<-- The regular expression ".+(?=\d{4})" means "find one or more character until the sub expression 'four consecutive digits' is matched to the right" -->
<-- By finding everything before the 4 digits, instead of looking for "everything up to 'digit' dot 'digit' and then 'underscore' we gain more flexibility when using CruiseControl labels to do BC builds -->
< GMS_1.1_BC_0058, Pawlowski_TestBuilds_0224, and GMS_1.1_0763 -->

Example buildnumberline:
<BuildNumber>0010</BuildNumber>

i5-6600K[delid]@4.8GHz/4.5GHz@1.385v | 2x4GB Crucial-DDR4-2133@14-14-14-30 1T 2800MHz@1.36v
ASUS Z170K | Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1355/1940 BiosMod

message edited by hidde663


Report •

Ask Question