Search This Blog

Sunday 4 May 2014

Rename xml files using the value of an xml-node


A customer wants to monitor a directory for new XML files and move/rename those files to a different directory. The files should be renamed to the value of an xml-node inside the xml file.

For example, the file 123.xml, which has the following contents

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="">

should be renamed to the value of the <PRMap> node, the 123.xml file will be renamed to I_32c_T_A.xml.


WatchDirectory does not directly support reading xml values, but we wrote a little helper program (GetFromXML.exe) to make this possible. The idea is to use the "Copy Files" task with the Dynamic Naming option.

One of the dynamic name tags allows you to call a batch script to determine (part of) the final name to use. This batch script can then read the xml file (using GetFromXML.exe) to extract the correct name to use.
Download GetFromXML here.

This is the batch script (GetFromXML.bat):

SET GET=C:\Bin\GetFromXml.exe
"%GET%" "%WD_FILE%" Title^|PRMap^|* > "%WD_FILTERRESULT%"
if %errorlevel% neq 0 (
  echo Error xml lookup in file fails > "%WD_FILTERRESULT%"

The script should be saved as C:\Program Files (x86)\WatchDirectory\Resolvers\GetFromXML.bat and now the customer can use the following "Dynamic Name" as the target for this copy files task: