Modify xml files in DOS batch processing and xml files in dos batch processing

Source: Internet
Author: User

Modify xml files in DOS batch processing and xml files in dos batch processing

In the past, batch processing and dos were only at the cd ping ipconfig level. I thought it should be very easy to change the file. If I read the file and replace it with another one, I will not write it back. Baidu's example will be changed, that is not a matter of minutes. It is much more complicated than you think.

I want to modify the content of this file:

....

Mylove = "******"

....

Mylove = "******" occupies a single line. I only need to modify the * part.

First, I want to use the set command to assign the content of a file to a variable. All kinds of attempts finally gave up. I thought the switch of the set command could be implemented. Which knows this product only supports operators, it is strongly recommended that expression is supported for the switch, similar to set/a xml = "type test. xml"

Read your code online and use the for command for file operations. Read and assign values to variables by row, as shown below:

Setlocal enabledelayedexpansion

Set xml = ""

For/f "delims =" % a in ('Type "mylove. xml "')

Do (set xml =! Xml! %)

This is true.

Note: to modify a declared variable, start the DELAY command setlocal enabledelayedexpansion, and change '%' '! '

Replace xml with set: replace xml with set, and write it back to the file. The problem is that the file I wrote contains '! 'Character, because the latency command is enabled, echo! Xml! > New. xml may cause problems, but the delay command can only be used properly.

Finally, I used a stupid method to write a row to a temporary file for each row. If I read the row I want to modify, I will write the new row into it.

Problems encountered during this period include:

1. echo> new. what is the echo output in xml? I wanted to use set: replace, but it was not clean. Finally, I was stupid. I first output one to a temporary file temp1.xml, repeat temp1.xml and filter echo ..., output empty line command: @ echo.> new. xml

2. garbled characters may occur in the output file because the file is originally UTF-8 encoded and gbk is used for output. Run chcp 65001 before output.

If the xml file you want to modify does not contain !, Empty rows do not contain Chinese characters or gbk encoding.

The Code is a bit dumb, so it is the result of achieving the desired result. Here, I want to share some code to help the people who need it.

The mylove. xml content of the file to be modified:

...

<! -- Once -->

Mylove = "Yu Hong"

...

Batch code:

Set newline = mylove = "Li Chang"

For/f "delims =" % a in ('findstr "mylove =" mylove. xml') do (

For/f "delims =" % I in ('Type "mylove. xml" ') do (

If % I ==%% a (echo % newline %> newlove. xml) else echo % I> newlove. xml) pause

I would like to thank all the comrades in the network for their answers and questions. If any of the above is wrong or unreasonable, I hope to correct them. O (∩) O Thank you


How to use a batch file to change the values in the xml file

For/f "tokens = * delims =" % I in (2.xml) do (For/f "tokens = 1-5 delims =" % j in ("% I ") do (Set str = If "% j" = "<OilTime" (Set "str = <OilTime Times =" 50 "TimesType =" 20 "/> ") if defined str (Echo! Str !) Else (Echo % I) If defined str (Echo! Str!> 2_new.xml) else (Echo % I> 2_new.xml) Pause

The dos batch processing command combines all xml files in Multiple folders into one file for storage.

Copy d: \ 11 \ *. xml d: \ sum. xml
Copy d: \ 22 \ *. xml d: \ sum. xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.