1

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__.

1
  • Being that we don't know what you're replacing the strings with and have no idea from your snippet if your XML file is encoded as UTF-16 or UTF-8, a pure batch file solution may be unwise. Commented Feb 19, 2018 at 20:08

1 Answer 1

1
@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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Magoo for your help.You suggested code is working fine for my requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.