Search code examples
xmlbatch-filecommand-lineformatxmllint

Batch pretty printing XML with XMLLINT.exe fails


I have many subdirectories with XML-Files all in one line. To pretty print them I use xmllint, which works perfectly. So I wanted to write a batch-script, which executes the xmllint.exe in all subdirectories. This is what I have so far:

setlocal enabledelayedexpansion

for /D /r %%d in (./*) do (
    pushd %%d

    for %%x in (*.xml) do (
        ren %%x %%~nx.old
        ::Get the filename, without the .old-extension
        set "filename=%%~nx"
        set "extension=.xml"    
        ::Concatenate filename and extension
        set "finalname=!filename!!extension!"
        %~dp0\xmllint.exe %%x >> !finalname!

        del %%~nx.old
    )
    popd
)

But i have two problems:

1.) When i execute this I get an error: Exception : System.IO.IOException: The Process can't access the file, because another process is using it.

But I checked I have everything else closed.

2.) When I tested it with single files I noticed, once I have a file, which is a bit damaged (for example misses one closing-tag), then it also gives me an error, that it can't format it, since the tag is missing.

Is there some option for xmllint, to surpress it an pretty print it nonetheless?

Thanks!


Solution

  • In your code, the path are incorrectly formatted. Also you have to quotes every xmlint arguments

    @echo off
    for /R %%a in (*.xml) do (
        rem :: make a copy
        copy "%%~a" "%%~dpna.old"
        "%~dp0xmllint.exe" "%%~dpna.old">> "%%~a"
        del "%%~dpna.old"
    )