Running Saxon from a batch file: how do I capture errors? - batch-file

I've got a batch file that calls Saxon to do an XSLT transformation. For some files, Saxon gives me an error. This appears in a new command line window which is open for about 1 second, and then closes. I'm trying to capture that error message in a file.
This is the relevant part of the batch file:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"file.xml" -xsl:fixerrors.xslt -o:"output.xml" 1>>fixerrors.log 2>&1
The fixerrors.log file is created, but remains empty even if Saxon encounters an error and creates the new command line window.
I was able to capture error messages from another program like this, so the idea of using 1>>fixerrors.log is not wrong in itself. This seems to be specific to Saxon.

Tried two approaches suggested by #Gerhard Barnard:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1 & type fixerrors.log
does not work, the Saxon errors are not placed in the log file.
"C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1
does work, the error messages are placed in the log file (in fact, they're placed twice because I'm redirecting both standard error and standard output to the file).

Related

Why does my IrfanView command not work on batch file but work when typing directly in CMD?

I'm a first-day user of IrfanView and have a question. I have a bunch of multi-page tiff files and I want to split all of them individually. So I write a batch file with the command like this:
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename2.tif /extract=(D:\newdirectory,tif)
...and so on...
I put the batch file on D drive, let's say in folder "batchfolder". But it can't do the job, this message shows up for each unsuccessful case (all of them were unsuccessful):
D:\batchfolder>C:\Program Files\IrfanView D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif) 1>i_view64.exe
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I guess that has something to do with the batch file location, so I bring it to C drive. But still it can't run properly, this time a different message shows up:
C:\>C:\Program Files\IrfanView D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif) 1>i_view64.exe
Access is denied.
This C:\>C:\ makes me think maybe the C:\ part on the batch file was redundant. So I take it out to make it look like this:
Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
...
But it doesn't work, either with the batch file on D or C drive.
I then try to type it directly in the CMD window and it works normally, like this:
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
Can you tell where my batch file goes wrong?
This is another question. Typing (or copy and paste) the batch file contents into the CMD works OK. But upon successful splitting, the original, multi-image file automatically opens. How can I deactivate this feature?
Note: Cross-post here: https://irfanview-forum.de/showthread.php?t=11150&p=47111#post47111. Hope it doesn't violate policy.
enclose paths/filenames with spaces into quotes to tell the interpreter, it's not two words, but one string (or even better: get used to always enclose path/filenames):
"C:\Program Files\IrfanView\i_view64.exe" "D:\originaldirectory\filename1.tif" /extract=("D:\newdirectory",tif)`
Before you build a batchfile with dozends or hundreds of nearly identical lines, use a for loop to process all .tif files in the folder:
#echo off
for %%a in ("D:\originaldirectory\*.tif") do (
"C:\Program Files\IrfanView\i_view64.exe" "%%~fa" /extract=("D:\newdirectory",tif)
)
see for /? for more information.
You need to call the executable with quotes in the batch. Also, the > in the path will not work either. Also consider using a for loop instead of creating single batch lines.
Please try this:
"C:\Program Files\IrfanView\i_view64.exe" "D:\originaldirectory\filename1.tif" /extract=("D:\newdirectory",tif)

Unable to execute a .bat file in post build event command line

My post build command is
call "$(ProjectDir)MyFile.bat"
And getting build error:
Error 1 The command "call
C:\MyProject\MyFile.bat"
exited with code 1. C:\Program Files
(x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets 4548 5
What am I missing here? Working on TFS source code, is that the reason getting this error?
For testing in MyFile.bat the only code is mkdir MYTestFolder, but then I am also getting the same error.
It seems like the syntax of the call is correct. Therefore I believe it's failing inside the batch file. I suggest putting the first few lines of the batch to write the time into a log file, so you can confirm it's being called. Do this before any of the actual work, so it can be sure that the batch is being executed.
call "$(ProjectDir)MyFile.bat"
MyFile.bat
#echo off
echo time /t > MyTempFolderPath\logfile.txt
It is the code inside the batch file that is failing - what is it doing?
You have a few options:
a - I would use process monitor and watch process exits and see the status codes of whatever the batch file launches.
b - You could also try running the batch file from the same folder as msbuild and see if you get any errors
Ed

Batch Files - Closing Opened Text Files

I currently have a Windows batch file that runs an .exe file that uses a text file. I am trying to have the Windows batch file run the .exe file multiple times. This, however, requires the use of the same text files to read from. The command prompt gives me the error that the ".txt could not be opened" (I assume from this that it is already open.)
I am trying to see if there is a way in a .bat file to system call to kill that specific text file. The suggestions I see online are to use 'taskkill notepad.exe', but that returns "invalid argument" because the program doesn't open Notepad to use the text file.
Any suggestions would be appreciated.
It sounds like your existing script fails because the first instance of the exe is still open when the second instance starts.
One thing worth trying (and this depends on the nature of the application you are invoking) is to start the executable using the START /WAIT /B ... command. This makes the command interpreter wait for the program to exit before it moves onto the next command, so as long as nothing else is locking the text files you should be OK to move onto the next command.

Windows Command Line 'Start' prevents finding all files

In a batch file, I have this line that runs an antivirus program. I'd like to ensure that it finishes its job before the batch moves on the following lines. From what I understand, START/WAIT is the wait to go. Unfortunately, START/WAIT breaks the line of code.
Here is the code that runs:
"C:\Program Files (x86)\Anti-Virus\" d:\files\%1\ProcessFiles\%2-proc\*.*
This code does not run:
START/WAIT "C:\Program Files (x86)\Anti-Virus\fsav" d:\files\%1\ProcessFiles\%2-proc\*.*
When this line runs, I get an error saying:
"Windows cannot find 'd:\files\abc\ProcessFiles\abc-proc\*.*'.
Make sure you typed the name correctly, and then try again."
While that error prompt is open, if I navigate to that folder, there ARE files in there.
Thanks in advance for any help.
using hint from SS64,
Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""
According to the Microsoft documentation, the title is optional, but you may will have problems if it is omitted.
START "" /WAIT "C:\Your Anti-Virus\antivir" "d:\files\%1\ProcessFiles\%2-proc\*.*
START/WAIT is not a single word, it's two seperated by a space.
Also, putting double-quotes around the file-specs will help if any have spaces in their names.
Try This:
START /WAIT "C:\Program Files (x86)\Anti-Virus\fsav" "d:\files\%1\ProcessFiles\%2-proc\*.*"

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources