Showing results for 
Search instead for 
Did you mean: 

IF statements for LabVIEWCLI Batch Files

Go to solution

Hi Team,


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>NI_App_Builder_API.lvlib:Build (path).vi->>>>>
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:
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?

0 Kudos
Message 1 of 5

Are you able to get the response string back into LabVIEW through one of the SystemCommand subVI outputs?

0 Kudos
Message 2 of 5
Accepted by topic author McQuillan

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.

LV 7.1, 2011, 2017, 2019
Message 3 of 5

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 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
EXIT %errorlevel%
Message 4 of 5
Accepted by topic author McQuillan

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[6]

$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[6] is the first line in the Operation Output and contains the file path for the executable. 

Michael Bilyk
Systems Engineer
National Instruments
Message 5 of 5