Make a Folder Manifest for XML Files

One task that has come up quite a lot as I’m work­ing with a lot of XML files (most­ly DITA con­tent) is I need a way to cre­ate a list of all the XML files with­in a fold­er. More than not, I want this list to be an XML file, too. There’s real­ly no fold­er- (or even file-) lev­el oper­a­tions in XSLT to do this. It’s sim­ply not what that lan­guage is used for. To do this, I had to cre­ate a sim­ple script. Using scrips like this is very easy to inte­grate into the DITA-OT (though not where I use this par­tic­u­lar script).

If you’re a web devel­op­er, there’s prob­a­bly many bet­ter ways to go about doing this than using a Win­dows Batch file. You prob­a­bly already know many of them. This isn’t intend­ed to be used in a web data sce­nario, but more for local XML data man­age­ment tasks.

The Windows Batch File

I per­son­al­ly real­ly like the Win­dows batch file com­mand lan­guage. It’s pret­ty sim­ple, even though it does lack a lot of nice fea­tures1. When you want to do fold­er or file oper­a­tions in Win­dows, I think it’s the eas­i­est thing to use even when you’re a real­ly poor pro­gram­ming like I am.

This batch file writes three pieces of infor­ma­tion to an exter­nal XML file:

  1. It writes a root node to an XML file. It also adds the fold­er path into an attribute of the root node, which can be use­ful for post-processing.
  2. For every XML file in the fold­er, it adds a child node after the root node’s open tag. These child nodes will con­tain a link to these XML files in the folder.
  3. It writes a close tag for the root note.

I refer to this new XML file as a man­i­fest, as it lists all fo the con­tents (well, XML files in this case, any­way) in the fold­er. Once an XML file is cre­at­ed with this infor­ma­tion, XSLT can then be used to use or change the infor­ma­tion in those files by run­ning against this man­i­fest file.

So, MakeManifest.bat looks like this:

SET output=manifest.xml
ECHO ^<manifest sourcepath="%~dp0"^> > %output%
FOR %%f in ("*.xml") DO (
    ECHO      ^<file href="%%~nf.xml"/^> >> %output%
)
ECHO ^</manifest^> >> %output%

Copy those lines into a plain text edi­tor and save it with the file exten­sion .bat and give it a try!. That’s all there is to it. If none of that makes any sense to you, I’ll refer you to SS64’s CMD ref­er­ence page.

It is worth not­ing that (and the sharp read­er might have fig­ured this out already) this list will include a refer­nce to itself, itself being anoth­er XML file in the fold­er. You could sim­ply rename the out­put file exten­sion to some­thing else (.txt, .man­i­fest, etc.), which is a good rea­son I put in a vari­able to make that easy to do. It does­n’t affect what’s in the file.

Post-Processing the Manifest File

In my case, these XML files tend to be DITA top­ics. What I’m real­ly after here is to cre­ate a DITA map. With a lit­tle XSLT file to process this man­i­fest —which can be run from the same Win­dows batch file— it’s easy to cre­ate a DTIA map for all of the DITA top­ics the script finds in the folder.

Now, to do this, I use Saxon9HE, which is the opens source ver­sion of Sax­on­i­ca’s (Michael Kay’s) XSLT proces­sor. It’s easy to use, very fast, sup­ports the lat­est ver­sions of every­thing, and free.

I’ll fol­low up this post with anoth­er soon about how to do just that. I want­ed to post this step first so as to not over­whelm some­one who is learn­ing (nor give me an excuse to put off post­ing anything).

  1. Most notably, to me, is reg­u­lar expres­sions. How­ev­er, the RxFind util­i­ty is a great way to add reg­u­lar expres­sion search and replace func­tion­al­i­ty to your Win­dows batch files and I use it a lot. []

By Jason Coleman

Structural engineer and technical content manager Bentley Systems by day. Geeky father and husband all the rest of time.

Leave a comment

Your email address will not be published. Required fields are marked *