Build events in C++ project - c++

I have a post-build event in my C++ Visual Studio 2010 project, which uses command xcopy, but when this xcopy return error code (>0), all build failed too and message "build unsuccessfull", how can i turn of error sensetivity in build events?
Thanks!

You can use the Exec task's IgnoreExitCode:
<Target Name="MyAwesomePostBuildTarget" AfterTargets="Build">
<Exec IgnoreExitCode="true" Command="xcopy etc. etc." />
</Target>

You can override the failure result of (almost) any batch CMD by appending || exit /b 0 to the end of the command. Example:
del somefile.txt || exit /b 0
In this way batch files work a bit like C. You can do && to conditionally run a command when the previous command succeeds, and || to run a command when the previous command fails.
exit /b 0 tells the CMD processor to exit the script and set the errorlevel to zero (0). Never forget to include the /b switch! Without it, CMD will exit the calling script as well as the current script which is rarely, if ever, the desired behavior.
I use this trick from the Visual Studio IDE, so there's no need to do low-level project hacking. And it fits on one line, which is also convenient from the IDE.
Another useful trick is silencing the command, by the way:
xcopy srcfile destfile 1>nul 2>nul || exit /b 0
1 is stdout, and 2 is stderr. Windows suite of shell programs are notoriously inconsistent with regard to which output they might use, so I generally just pipe both or pipe neither.

Related

Visual Studio 2017 Post Build Event does not generating an error when compiling

I have 3 Post Build Events, the first event is invalid and should create an (exited with code 3) error when compiling my application. The question I have is why is VS 2017 not throwing the error when I compile my application?
if I remove the 2 xcopy events and leave the invalid event as the sole event in my Build Events then VS shows the error as expected but only when it is the sole build event.
My Build Events look like this (below), where the path to SignTool.exe is indeed invalid. The build event does indeed fail (the exe is not signed) but no error is reported unless I remove the 2 xcopy commands, Can anyone explain?
"C:\Program Files (x86)\Microsoft SDKsZZ\Windows\v7.1A\Bin\SignTool.exe" sign /f "C:\Data\Visual Studio\CodeSigningCertificate\Cert2020.pfx" /p deznads $(TargetPath)
xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Debug\*.* /y
xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Release\*.* /y
Apparently, Visual Studio doesn't parse the post-build instructions into a set of individual commands, but rather executes it as a single batch file, then checks the final %ERRORLEVEL% to decide if it failed or succeeded.
In your example there are 3 commands and each would set %ERRORLEVEL% based on its own success or failure, so if commands #1 and #2 fail, but #3 succeeds the #3 result will override prior errors and the Studio will consider this a successful post-build step.
You can make your build event to stop on first error, but that's something you need to implement there. For example:
"C:\Program Files (x86)\Microsoft SDKsZZ\Windows\v7.1A\Bin\SignTool.exe" sign /f "C:\Data\Visual Studio\CodeSigningCertificate\Cert2020.pfx" /p deznads $(TargetPath)
if %ERRORLEVEL% EQU 0 xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Debug\*.* /y
if %ERRORLEVEL% EQU 0 xcopy $(TargetPath) E:\Data\RotoApps\WIP\bin\x86\Release\*.* /y
Here if command #1 or #2 set non-zero %ERRORLEVEL%, the batch won't execute the following commands and retains the error code in %ERRORLEVEL% variable, which will be reported by Visual Studio as a failed post-build step.
You can also implement more complex logic there, decide if the post-build event failed or not, and tell that to Visual Studio by returning 0 on success or other value on error with the exit N command.

How do I check if a compiler error shows in cmd?

I decided to make it easier for me to compile and run my C++ programs through command prompt (I use cmd in conjunction with Sublime Text).
I added a cmd command (dragged .bat file in system32) to jump to my C++ files directory, and then a few more commands to compile my three work files.
I wanted cmd to output "Compiling work.cpp..." (which I've implemented) and if it doesn't return an error, output "Successfully Compiled" (What I don't know how to do). Is there anyway to implement this in cmd?
I'm using g++ (tdm64-1) 5.1.0
Use the errorlevel "environment variable":
g++ work.cpp
if %errorlevel% == 0 echo "Successfully Compiled"
This uses the syntax of environment variables (the % part), but errorlevel is a special "variable", which holds the status of recently finished command. If the status is 0, it was successful, otherwise an error occurred.

want to apply the try catch for the batch file(alternative of try catch)

i want to dump the data excluding contenttype. I am gerring error so that i wnt apply try catch to code.
So that i can find out what error i am facing.
set /p pathName=Enter The path where you want to take backup:%=%
#echo %pathName%
set d=%date:~-4,4%_%date:~-7,2%_%date:~0,2%
set d=%d: =_%
set t=%time:~0,2%_%time:~3,2%_%time:~6,2%
set t=%t: =0%
md %pathName%\media
try{
python dtz/manage.py dumpdata -e contenttype>> %pathName%/Backup_on_%d%_%t%.json
}
catch
{
python dtz/manage.py dumpdata>> %pathName%/Backup_on_%d%_%t%.json
}
pause
xcopy dtz\templates\media %pathName%\media /s
Error i am getting CommandError: Unknown app in excludes: contenttype
Please help me out.
There are no try-catch blocks in batch scripts. You need to use the errorlevel returned by a command to check whether it executed successfully or failed. Generally errorlevel 0 indicates that the command executed successfully, but the errorlevel can also depend on the command/program being executed.
Details about ERRORLEVEL.
ERRORLEVEL can be used as follows:
IF %ERRORLEVEL% NEQ 0 (do something)

Informatica Command Task

I asked this question on the Informatica forums, but no one knew the answer, so now I'm coming here for some help.
To my understanding, anything in a "post-session success command" field should be passed to the appropriate command interface and executed. However, when I use an IF statement, it fails. Any ideas?
"IF 1==1 echo.bob >> f:\filename.txt"
This works when I type it manually into the terminal (DOS in this case). But when I throw it into a reusable command task, I get this:
ERROR
POST-SESS
CMN_1949
Error: [Pre/Post Session Command] Process id 2996. The shell command failed with exit code 1.
PS: Using 9.5.1 Hotfix 1
I think the problem is not in how Informatica executes commands. The problem lies in how DOS return error codes, and specifically that some commands, like IF and ECHO does'nt. (The return code Informatica picks up from DOS can be seen with echo %ERRORLEVEL% in DOS, and I'll use the name DOS here for convenience even though under Windows now this is'nt strictly correct)
Run these commands in succession:
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 0
cd c:\
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\filename.txt
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\thisdirdontexist\filename.txt
echo %ERRORLEVEL%
The first CD set a return code, in this case to 0.
The following ECHO (with or without the IF test) does not change the return code, thus it remains 0 even though the last ECHO fails.
If the first CD command would have returned an error;
#echo off
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 1
cd xxxxxx
echo %ERRORLEVEL%
then all the subsequent ECHO would return 1 and Informatica would fail them both.
This said it is still strange since each post-session success command in Informatica is executed under its own cmd-shell, so the initial ERRORLEVEL for every command should allways be 0. I can't explain that and unfortunately I can't actually test this in Informatica as we run under UNIX, but I'm pretty sure this is at least part of the problem.
To get around the problem you should make sure that you set the "Fail task if any command fails" option on the property-tab. This makes Informatica use the cmd/c option and since this set a proper return code Informatica should be able to pick up the error (or success) correctly. If this still doesn't work properly try change the command yourself to:
cmd /c "IF 1==1 echo.uncle >> c:\filename.txt"

Windows Batch Commands - Advanced Error Suppression Issue

I'm programming a very large purpose-driven Windows command prompt batch program.
The problem is that I can't suppress the error's text. I could "cls" but that means that almost every command will cls the prompt and I don't want to force that on users. I also tried using >nul after it, and 2>nul in front of the command. The problem with 2>nul CMDOW.EXE /RUN is that then it doesn't store the error in the ERRORLEVEL environment variable.... I also can't have this error info showing up almost every time they use a command in the prompt.
My console does tons of things, including quick navigation and web-page/program/folder access. Recently I have been trying to implement a basic wrapper around the central batch program so that if you type something that is an unrecognized command, it will first check to see if the text string you input is the beginning of a folder's name within your current directory. If so, it will auto-move you into the folder. If not, it will display the usual error message.
I made it so that the input is no longer standard dos input, but a set /p command with a prompt that imitates the usual interface. I got it so it doesn't wait when typing a program name when not preceded by "start" if its in a PATH using CMDOW.
Everything functions now except I have a small issue that's a large visual nuisance. I prompt the user and store the input to an environment variable, then use CMDOW.exe /RUN to first attempt to execute the input text and see if it's an executable file (this covers paths, as well as .exes in the current directory). I then check ERRORLEVEL to see if this resulted in an error. If so, I move on to the next method.
I've also tried writing a little C++ program to execute for me, it works easily with winexec but idk how to obtain error code to confirm the process started sucessfully. If I can do that, then I can just send that result to an environment variable. CreateProcess() doesn't work without being absolute with the location. I can't just plug in the input text and have it work but it pauses until termination anyway I believe. ShellExecute() works but doesn't seem as simple as plugging it in from input... though it appears to have a ready method of obtaining the output. I might add I'm not great at C++, I learn what I need to to get by.
I'm not sure how to get around this issue. I'm also not sure if there exists some special method to bypass that error output while still gaining the knowledge that it didn't execute properly.
Here is my code:
#SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
#for /L %%i in (0,0,0) do #(
set zinput=
set /p zinput=^%time%[%cd:~0,1%]^>
call :EXECUTE
set zinput=
)
:EXECUTE
IF ("%zinput%")==("") GOTO :EOF
cmdow /run "%zinput%"
set ERRCODE=%ERRORLEVEL%
IF ("%ERRCODE%")==("1") call :UNDEFINED
GOTO :EOF
:UNDEFINED
%zinput%
set ERRCODE=%ERRORLEVEL%
IF %ERRCODE%==9009 GOTO ZDIR
GOTO :EOF
:ZDIR
set zDIR=
DIR /B /AD-H|sed -n "/^%zinput%/"Ip>"%aicnspath%\etc\dump.txt"
set /p zDIR=<"%aicnspath%\etc\dump.txt"
IF NOT DEFINED zDIR GOTO UNDEFINED2
cd %zDIR%
call "%aicnspath%\etc\update.bat"
cls
echo AUTO-MOVED INTO: %CD%|tr '[a-z]' '[A-Z]'|sed "s/^/%_hc%%_bc2%/"|sed "s/$/%_bc%/"
echo -----------------------------------------
echo/
GOTO :EOF
:UNDEFINED2
cls
echo/
echo The string %_hc%%_bc2%^'%zinput%^'%_bc% is not recognized by AICNS as any internal or external command, operable program or batch file.
echo/
GOTO :EOF
Try this...
CMDOW /run "%zinput%" >nul 2>&1
You should still have access to the ERRORLEVEL after this.
I don't see any reason because 2>nul CMDOW.EXE /RUN don't return the ErrorLevel value whereas CMDOW.EXE /RUN return it, unless CMDOW.EXE was written this way; this sound very strange to me.
The Batch file below check if a command is an executable file and return these values via ErrorLevel: 0 if not found, 1 if found in current directory, and 2 if found in a directory of PATH variable. I hope you can use a modified version of this Batch file instead of CMDOW.EXE program to solve your problem.
PATHOF.BAT:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM CREATE A LIST OF FILE NAMES ADDING THE EXECUTABLE EXTENSIONS
SET NAMEEXT=!PATHEXT:.=%1.!
REM SEARCHES FILE NAMES IN CURRENT DIRECTORY, IF FOUND: ERRORLEVEL=1
FOR %%N IN (%NAMEEXT%) DO IF EXIST %%N ECHO %%N & EXIT /B 1
REM SEARCHES FILE NAMES IN DIRECTORIES OF PATH VARIABLE, IF FOUND: ERRORLEVEL=2
FOR %%N IN (%NAMEEXT%) DO IF NOT "%%~$PATH:N" == "" ECHO %%~$PATH:N & EXIT /B 2
REM IF FILE NOT FOUND, ERRORLEVEL=0
ECHO '%1' is not an external command or batch file located in PATH & EXIT /B 0
Please, let me know if this method worked or if you got an additional problem using it.