Search code examples
http-redirectbatch-filexmllint

How can I redirect the output of a command (running in a batch loop) to a file?


I have a windows batch file, which iterates over files in a folder and runs a command on each file. Specifically I am running xmllint to validate some files:

for %%i in (c:\temp\*.xml) do (
   C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt
)

It currently shows the output on screen. I want to show the output of all these commands placed in an output file. How can I achieve this? By using the append operator (>>) nothing is accomplished except for a blank file being created.

Is it because of xmllint?


Solution

  • If you're trying to redirect error output from the program, it might be writing to stderr. You can try to redirect it with:

    for %%i in (c:\temp\*.xml) do (
       C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt 2>&1
    )
    

    Basically the 2>&1 at the end means redirect anything from stderr (which is 2) to stdout (which is 1). Since stdout is redirected to a file, you should now see the stderr stream in the file. Hope this works for you!