Pankaj Karmakar
Pankaj Karmakar

Reputation: 89

How to update xml file parameter using .bat file?

I am new to windows batch programming.

I have the below XML file

<itp:params>
  <!--1 or more repetitions:-->
  <itp:param name="persid">PERSID__</itp:param>
  <itp:param name="attmnt">ATTMNTID__</itp:param>
</itp:params>

Now I have to replace the parameter PERSID__ & ATTMNTID__ using batch file.

I am able to update the PERSID__ using the following code

set _ATTMNTID=%1
set _PERSID=%2
setlocal DisableDelayedExpansion
set "search=PERSID__"
set "replace=%_PERSID%"

for /F "delims=" %%a in (%_XML_TPL%) DO (
   set line=%%a
   setlocal EnableDelayedExpansion
   >> %_TEMP_FILE% echo(!line:%search%=%replace%!
   endlocal
)

But struggling to update the second param ATTMNTID__.

Upvotes: 1

Views: 382

Answers (1)

Magoo
Magoo

Reputation: 79983

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q48861626.txt"
SET "outfile=%destdir%\outfile.txt"

set _ATTMNTID=%1
set _PERSID=%2
setlocal DisableDelayedExpansion
set "search=PERSID__"
set "replace=%_PERSID%"
SET "SEARCH2=ATTMNTID__"
SET "REPLACE2=%_ATTMNTID%"

for /F "delims=" %%a in (%filename1%) DO (
   set line=%%a
   setlocal EnableDelayedExpansion
   SET "line=!line:%search2%=%replace2%!"
   ECHO !line:%search%=%replace%!
   endlocal
)>> %outfile%


GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances.

I used a file named q48861626.txt containing your data for my testing.

Produces the file defined as %outfile%

Simply cascade the substitutions.

Note the use of quotes in set commands to ensure that trailing spaces on the line are not included in the data assigned.

Note the placement of the redirection to the output file. It gathers all echo output from the code block and redirects to the file, so > could be used here in place of >> to create a new file if desired.

Upvotes: 1

Related Questions