From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

IF statements for LabVIEWCLI Batch Files

Solved!
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 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?

0 Kudos
Message 1 of 5
(2,777 Views)

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

0 Kudos
Message 2 of 5
(2,722 Views)
Solution
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.

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 3 of 5
(2,718 Views)

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%
Message 4 of 5
(2,685 Views)
Solution
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
Former NI Software Engineer (IT)
Message 5 of 5
(2,414 Views)