I'm creating a batch file for automating tests and builds (using Jenkins) but I'm struggling to work out how to to do a basic IF statement if the build fails.
Basically, If a build fails, I want to EXIT <error code>, ELSE carry on with the rest of the script
If I send a command like this:
LabVIEWCLI -OperationName ExecuteBuildSpec -ProjectPath "<path>" -TargetName "My Computer" -BuildSpecName "exe"
and it fails, it will return something like this:
Operation output: Error Code : 7 Error Message : Invoke Node in GetTargetBuildSpecs.vi->NI_App_Builder_API.lvlib:Build (path).vi->ExecuteBuildSpec.lvclass:RunOperation.vi:5910001->CoreOperation.lvclass:CallRunOperation.vi:4230002->ExecuteRunOperation.vi:5250001->ExecuteOperation.vi:6900002->ExecuteOperation.vi.ProxyCaller <APPEND> Method Name: <b>Project:Open</b> An error occurred while running the ExecuteBuildSpec operation. ExecuteBuildSpec operation failed.
Or if it is successful it will return this:
Operation output: Generated files are: C:\Users\Software\Desktop\testcode\builds\exe\Application.exe C:\Users\Software\Desktop\testcode\builds\exe\Application.aliases C:\Users\Software\Desktop\testcode\builds\exe\Application.ini ExecuteBuildSpec operation succeeded.
Can someone let me know, how based on the return data I can use simple if statements to either 'stop and return an error code' or continue with the rest of the script?
Solved! Go to Solution.
Never used LabVIEWCLI. However, usually console programs return error information into the environmental variable %ERRORLEVEL%; there is plenty of tutorials and examples about this, see for example this link (picked almost a random). Check the statement 'IF ERRORLEVEL n' to react to errors. Statement 'GOTO label' will also help you to contnue processing elsewhere in the batch file.
Thank you pincpanter,
Your solution was simple and worked straight away.
For the benefit of others this is how I implemented the solution:
@echo on rem :: This section is to set batch file variables SET PROJECTNAME=<projectname>.lvproj SET EXEBUILDSPEC="<exename>" SET INSBUILDSPEC="<installername>" rem :: This is the main script LabVIEWCLI -OperationName ExecuteBuildSpec -ProjectPath "%cd%\%PROJECTNAME%" -TargetName "My Computer" -BuildSpecName %EXEBUILDSPEC% IF %errorlevel% NEQ 0 ( goto error ) LabVIEWCLI -OperationName ExecuteBuildSpec -ProjectPath "%cd%\%PROJECTNAME%" -TargetName "My Computer" -BuildSpecName %INSBUILDSPEC% IF %errorlevel% NEQ 0 ( goto error ) LabVIEWCLI -OperationName CloseLabVIEW IF %errorlevel% NEQ 0 ( goto error ) :error EXIT %errorlevel%
Just in case other people see it.
$? also returns a boolean value about whether it failed or succeeded.
Additionally, the return values and paths can be accessed like this:
$buildOutput = LabVIEWCLI -OperationName ExecuteBuildSpec -ProjectPath... echo $buildOutput
$buildOutput is an array where each item is a line from the output of the LabVIEWCLI command (note that this will prevent those lines from displaying in the console)
You can access the information in those lines as a standard array. For instance, $buildOutput is the first line in the Operation Output and contains the file path for the executable.