VBScript to copy specific contents from an xml file and paste it in another



Ok, I have 2 XML files. I need to copy some specific data from one to the other. This will be determined by one of the tags within the xml.


This will be run from a batch script, which needs to pass over the variable to search for. I originally tried to accomlish this in the batch script itself, but it got very messy, very quickly and I don't even know if it is possible.


Here is an example:


So the batch script has the variable %cata% which is Category A and it needs to pass that to the VBScript first.


Then the VBS needs to search for all Categories tagged Category A. It must then copy all items in this category (Everything between and including the <Item> </Item> tags) and paste them into a similar xml file.


XML 1:



<?xml version="1.0" standalone="yes"?>
<Program>
<Item>
<ID>01</ID>
<Title>Item 1</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<ID>02</ID>
<Title>Item 2</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<ID>03</ID>
<Title>Item 3</Title>
<var1 />
<var2 />
<Category>Category B</Category>
<var3 />
<var4 />
</Item>
<Settings>
Various Settings
</Settings>
</Program>


XML 2:



<?xml version="1.0" standalone="yes"?>
<Program>
<Item>
<ID>04</ID>
<Title>Item 4</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<ID>05</ID>
<Title>Item 5</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Settings>
Various Settings
</Settings>
</Program>


After the script has run, XML 2 should look something like this:



<?xml version="1.0" standalone="yes"?>
<Program>
<Item>
<ID>01</ID>
<Title>Item 1</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<ID>02</ID>
<Title>Item 2</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<Item>
<ID>04</ID>
<Title>Item 4</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Item>
<ID>05</ID>
<Title>Item 5</Title>
<var1 />
<var2 />
<Category>Category A</Category>
<var3 />
<var4 />
</Item>
<Settings>
Various Settings
</Settings>
</Program>


Now I would be very happy just to get that part done, but there is more.


Each of these items has a corresponding folder named <Title>.39DJOU008(string of varying numbers/letters)JJ8SBD9 (residing in a different directory)


Each item that is copied from the xml file, also needs the corresponding folder copying to a new destination.


Too much?


Thanks for the help and the answers.


No comments:

Post a Comment