0

G'day all. I have some code to random generate destination tags for a program I am creating. I am using CMD for this as I've already have 90% of the coding done. up to this point.

What I have is the program pulls 20 random tags and saves them as a variable. I have it set up in a loop and appends a text file every time it runs. Also I have a counter to tell me how many tags have been input into the saved text file.

Now, comes the tricky part. I need the program to update an .XML file that is already structured. All specific nodes of the xml must remain or else the main program that uses the .XML will not load the file properly. My Objective here is updating this node with the random generated 'tags' and input them in the said .XML file.

:Main
Set tags1=%Random%
    If /i %tags1% GTR 6 goto Main           
If /i %tags1% LSS 1 goto Main
goto Maniaa

:Maniaa
If %tags1% == 1 set tags2=NA
If %tags1% == 2 set tags2=FR
If %tags1% == 3 set tags2=LA
If %tags1% == 4 set tags2=BR
If %tags1% == 5 set tags2=SY
If %tags1% == 6 set tags2=BU
::This block of code is the random tag generation.


::This will echo out the saved tags
Echo ^<destinationTag^>%tags2%^</destinationTag^> >> tags2.txt

goto Main2

:Main2
::Setting up the counter within the tags2.txt 
set file=tags2.txt
set /a cnt=0
for /f %%a in  ('type %file%^|find "" /V /C') do set /a cnt=%%a
Echo %file% has %cnt% Random Generated Tags, Current %tags2%
::Once program reaches the required tags we can exit program

::THIS PART IS WHAT I AM TRYING TO DO.
for /f %%i in ('findstr "<destinationTag>%tags2%^</destinationTag^>" 
tags2.txt') do echo ^<destinationTag^>%tags2%^</destinationTag^> >> CSSE- 
2X.xml

ping -n 1 localhost >nul
goto Main
cls

Here is a snippet of the .XML I am working with, all others MUST remain the same. Just the "destinationTag" nodes need to be updated with %tags2%.

<reverseDirection>
        <boolean>false</boolean>
        <boolean>false</boolean>
      </reverseDirection>
      <loadWeightUSTons>0</loadWeightUSTons>
      <destinationTag>EL</destinationTag>
      <unitNumber>927453</unitNumber>

The iteration of destinationTags could be over 100 times in one XML file. There should be a loop process that I can use to update the said .XML file?

The main CMD program is running perfectly, the only issue is it's not updating the .XML file.

Any help is greatly appreciated.

  • I summarized once [here](https://stackoverflow.com/a/51579256/3074564) what must be taken into account on using `cmd.exe` with the help of `findstr.exe` for processing a text file. I understand that you have most of the work done already with Windows commands in cmd syntax and so you want to do the remaining task also with cmd. But I recommend to use [JREPL.BAT](https://www.dostips.com/forum/viewtopic.php?f=3&t=6044) which is a batch file / JScript interpreted using `cmd.exe` and `cscript.exe` to run a JScript regular expression replace on a file. – Mofi Sep 29 '19 at 15:46
  • `call "%~dp0jrepl.bat" "().*?()" "$1%tags2%$2" /F "tags2.txt" /O -` can be used with __JREPL.BAT__ stored in same directory as your batch file to replace the values of all occurrences of XML element `destinationTag` in file `tags2.txt` by the string assigned to environment variable `tags2`, i.e. change country/language abbreviation respectively add it if there is no string between start and end tag of XML element `destinationTag`. – Mofi Sep 29 '19 at 15:57
  • Thanks for your replies. I used the jrepl.bat program, but now it only outputs and appends 1 random tag for every loop. IE: LA. I would learn the jrepl.bat program, how ever I have limited time. I am wondering if there is code within cmd for this. – user3058775 Sep 29 '19 at 18:35

0 Answers0