how to take cmd output error message as an input to a batch file? - batch-file

i have a oracle sqldeveloper file.
when i run it by using a batch file, if it encounter an error, the cmd will surely display the error.
what i want is, is it possible to take the cmd error as an input to trigger out my custom made error message??
if in cmd displaying any error for example:
Parameter not found
my custom-made error message in batch file will triggered out this error message in the cmd:
You Got An Error!
any help and suggestions are appreciated..
Thanks in advance!

You can redirect the output (both stdout and stderr) to a text file (or to nul if you don't care about the output), and check the value of `%ERRORLEVEL% after your script has been executed. If it is not 0, then display your own error message.
To redirect both stdout and stderr:
command parameters 1>>c:\log.txt 2>&1

I recommend using SQL*Plus instead of SQL Developer. You can add WHENEVER SQLERROR EXIT FAILURE to the top of the script (or EXIT SQL.SQLCODE if you are interested in the specific error code). Then the calling batch file will receive the error code in %ERRORLEVEL% and you can take action appropriately. There is also WHENEVER OSERROR EXIT FAILURE if your SQL script interfaces with the OS with the potential of failure there.
I don't think SQL*Plus uses stderr, so I don't think you need to worry about redirecting 2>&1.

Related

Error while trying to write inputs to running console app with a batch

I have an exe file that I want to execute with a batch file.
Exe file is a third party console app that will prompt for input three times.
So I want to fill it in with an empty line, someText and someOtherText
(echo. echo someText echo someOtherText) | call config.exe remove
But I'm getting this error:
Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
What could be the issue, please advise at least the direction to search?
I have the same error even if try to execute a simpler command like:
echo. | call config.exe remove
Your config.exe does not accept redirection at all. It requires real console buffers.
If you want to automate it, you could create another console application which will
start your config.exe using CreateProcessW
write your lines to STD_INPUT_HANDLE using WriteFile (ASCII, \n separated lines) or WriteConsoleInputW (if you need unicode characters)
after write is complete, just wait for config.exe termination and return exit code
Another simpler option is ConEmu terminal (I'm the author). There is GuiMacro scripting which could be easily used from console. So you could just run from your prompt or wrap in in batch file (run in ConEmu):
ConEmuC.exe -GuiMacro print "\nsomeText\nsome Other Text\n" & config.exe remove

Windows Pre-commit hook to get Log message on Subversion with Tortoise

I have SVN and Tortoise SVN installed in my machine. I have created a Batch file".bat". I am trying to get Log message to validate it that was provided in the UI before commit.
I tried this answer which was failing with following error.
svnlook: E720003: Can't open file 'C:\Users\GOPICH~1\AppData\Local\Temp\svnC891.tmp\format': The system cannot find the path specified.
Can anyone guide me what is the issue is (or) is there anyway to achieve the same.
I had issues with svnlook log as it appends the \format.
Arguments returned from stderr to the commit failed dialog are known as:
path depth messagefile cwd
These arguments are printed from using the code in this answer when commit fails.
The 1st and 3rd are .tmp files located in the temp directory. (The 1st is a list of files to commit).
The 3rd argument is the message file so you can use that to check if a message is used.
#echo off
findstr . "%~3" >nul
if errorlevel 1 echo args: %* >&2 & exit 1
exit 0
This causes commit failure if message file is empty else allows the commit to succeed.
Issues:
Only a single space in message file causes commit failure.
Only a double space in message file allows commit.
Seems findstr pattern could be improved. This could be a personal preference so keeping the linked answer pattern from the question.
Idea aschipfl shared in comments:
#echo off
if %~z3 gtr 0 (exit 0) else exit 1
Any character in message file makes the file not zero in size.
AFAIK, stdout is redirected as being a hook script, thus unavailable,
so #echo off is possibly not needed.
Note: Tested with SVN-client which may be why svnlook log
gave me strange results. Thanks to aschipfl for the information.

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

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).

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

System cannot find the path specified

When I execute my batch file and write the output to a text file, I am getting error message "system cannot find the path specified" in the command prompt. I have to find which line in my batch script, throws this error or the path which is not found.
Note: As I have so many lines in my script, I cannot go through each line and check for the path.
I have to get the path or the line which causes this error when this error comes.
The quickest way to debug an error like this would be to
Remove #echo off if you have that at the top of your script
Remove any >nul's or similar you have for any commands
Add a pause at the very end of the script
Now run the script and look at the output for each of the commands until you find the error, and then you can see which command it did it for.
Look the first place in your batch that requires the path, i.e., a call like "copy" or "del", create a new line after it and put exit. Repeat further down the batch file until the problem appears.

Resources