How to Combine XML Files in Batch

Dr. Muhammad Abdullah Feb 02, 2024
  1. XML Files
  2. Copy Multiple XML Files Into a New XML File Using the copy Command
  3. Copy Multiple XML File Into a New XML File Using the <root> Tag
How to Combine XML Files in Batch

This article will first discuss and understand the XML file format. After this, we will discuss combining two or more XML files into one file using the Batch command and script.

XML Files

XML (also known as Extensible Markup Language) is a markup language file format to store and communicate the data. It uses a structured layout for data storage. XML files consist of tags and text; tags represent the structure, whereas text represents the data.

A sample XML file is shown as follows:

<note>
    <heading>Hello World</heading>
    <body>This is a sample XML document</body>
</note>

In the above XML document, <note>, <heading>, and <body> are three tags, and Hello World and This is a sample XML document are the text or data.

The file extension of a XML format is .xml.

Copy Multiple XML Files Into a New XML File Using the copy Command

We can combine multiple XML files into one new XML file using the copy command, as shown in the following:

copy *.xml new-combine-file.xml

The above copy command takes all the XML files using *.xml as a source and creates a new XML file new-combine-file.xml by combining all XML files’ content.

For example, consider the following three XML files placed in the same folder:

The first.xml file:

<note>
<heading>First</heading>
</note>

The second.xml file:

<note>
<heading>Second</heading>
</note>

The third.xml file:

<note>
<heading>Third</heading>
</note>

After running the command copy *.xml, new-combine-file.xml in the command line, it creates a new file with the name new-combine-file.xml consisting of the following contents:

<note>
<heading>First</heading>
</note>
<note>
<heading>Second</heading>
</note>
<note>
<heading>Third</heading>
</note>

The above content is combined using the above three files (first.xml, second.xml, and third.xml).

We can also combine these files using a Batch script. We must create a Batch script file containing the same copy command.

Copy Multiple XML File Into a New XML File Using the <root> Tag

Using a Batch script, we can combine multiple XML files into one file with a single <root> tag. Consider the following Batch script file combine-xml-file.bat:

@echo off
echo ^<root^> > new_xml_file.txt
type *.xml >> new_xml_file.txt
echo ^<^/root^> >> new_xml_file.txt
ren new_xml_file.txt new_xml_file.xml

In the above script, the first command, @echo off, is used to hide all commands from the command prompt. The next line echo ^<root^> > new_xml_file.txt creates a new file new_xml_file.txt with a text <root>.

The type *.xml >> new_xml_file.txt command appends all XML files content in the new_xml_file.txt file. Next, echo ^<^/root^> >> new_xml_file.txt appends the ending tag </root>.

Lastly, the new_xml_file.txt new_xml_file.xml command is used to rename the new_xml_file.txt file with the new_xml_file.xml.