03-18-2020 07:22 AM
Hello,
I'm trying to create a script where it checks the loaded filename and performs the correct function based on the file loaded.
The data files contain "TestCase..." at the end with test cases 1-17 to choose from.
I'm currently using the "InStr" function within a for loop (1-17) to check the the test case in the filename, but i'm having an issue where for example, if the test case is 12 in the filename, the script will carry out the function for the 2nd test case.
I believe (not 100% sure) that the function is looking for either 1 or 2 rather than 12.
I'm just wondering if there is a better method or function for this?
Any help is much appreciated!
Solved! Go to Solution.
03-24-2020 12:33 PM - edited 03-24-2020 12:34 PM
Hi Luke,
First off, I can greatly recommend indentation in your VBScript, so that you can better see boundaries of nested structures such as FOR loops and IF statements.
Secondly, and in direct answer to your question, I suggest that you use the Mid() function instead of InStr() to isolate a file name suffix that starts at a known string position in the file name. Let's say that your "TestFileName" variable has the following possible values:
TestFileName = "TestCase1"
TestFileName = "TestCase2"
TestFileName = "TestCase12"
TestFileName = "TestCase17"
This is how I would recommend branching your code based on the numeric suffix, starting at position 9:
Suffix = Mid(TestFileName, 9)
IF Suffix = "1" THEN
Call Action_1()
ElseIF Suffix = "2" THEN
Call Action_2()
ElseIF Suffix = "12" THEN
Call Action_12()
ElseIF Suffix = "17" THEN
Call Action_17()
END IF
Brad Turpin
Senior Technical Support Engineer
National Instruments
03-25-2020 05:02 AM
Hi Brad,
Unfortunately i still face issues with this method. The problem i have now is that the string position isn't always the same, for example the structure of the filenames used has the following format "TestProcedureNumber_TestDescription_TestCase".
The potential filenames could be:
5.5.1.3.8_Flap Enable PartB_TestCase4
5.5.1.3.26_Flap Enable PartB_TestCase14
As you can see, the string position can potentially change depending on the number of tests. Is there a method that can be used which doesn't require a known string position?
Thanks,
Luke
04-17-2020 06:21 PM
Hi Luke,
For delimited strings like this, I suggest tokenizing with the VBScript function Split():
Text = "5.5.1.3.8_Flap Enable PartB_TestCase4"
Text = "5.5.1.3.26_Flap Enable PartB_TestCase14"
Cols = Split("_" & Text, "_")
n = UBound(Cols)
IF n = 3 THEN
StepID = Trim(Cols(1))
StepName = Trim(Cols(2))
StepLoc = Trim(Cols(3))
END IF
Brad Turpin
Senior Technical Support Engineer
National Instruments