Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fix the IF/THEN statements?

Status
Not open for further replies.

feipezi

IS-IT--Management
Aug 10, 2006
316
US
Hello folks,

Happy New Year!

I have a folder like the attached. Then I have a small process in VBS trying to find out if SAS9 exists but it's not working. Here is part of the code.


....
....
For Each file in folder.Files
name = LCase(file.Name)

If InStr(LCase(Name), ".sas") Then pgm = Name
If InStr(LCase(Name), "sas 9") Or InStr(LCase(Name), "sas9") Then
sasflag = "9"
MsgBox "Run SAS 9"
Exit For
Else
sasflag = "8"
MsgBox "Run SAS 8"
Exit For
End If
'msgbox name & " " & pgm
Next
....
....


The msgbox statements show !!!!USE SAS 9!!!! is there but won't pick it up; still telling me Run SAS 8. What's going on? Did I do something wrong in the IF/THEN statements, or InStr() not working in a situation like this?

Please help me out there.

Thanks in advance.
 
 http://files.engineering.com/getfile.aspx?folder=046cbbab-3a51-443b-9d63-d780f8ae3f46&file=snapshot_on_folder.PNG
Hi,
Code:
For Each file in folder.Files
   name = LCase(file.Name)

   If InStr(LCase(Name), ".sas") Then pgm = Name
      If InStr(LCase(Name), "sas 9") Or InStr(LCase(Name), "sas9") Then
         Sasflag = "9"
         MsgBox "Run SAS 9"
         Exit For
      Else
         Sasflag = "8"
         MsgBox "Run SAS 8"
         Exit For
      End If
'[b][highlight]MISSING End If SOMEWHERE[/highlight][/b]
'msgbox name & " " & pgm
Next
....
....

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Thanks Skip for the quick return!

Sorry but I did not put down "End If" but that did not cause the problems. I guess the culprit is "Exit For" statement. If it did not find "SAS9" on the first file, then it will stop checking, taking "SAS8" for granted because of "Exit For", even the 2nd file contains "SAS9" but it won't get the chance of being checked.

To solve the problems, I tried this (maybe you have better idea, please feel free to share).


....
....
For Each file in folder.Files
name = LCase(file.Name)

If InStr(LCase(Name), ".sas") Then pgm = Name
If InStr(LCase(Name), "sas 9")>0 Or InStr(LCase(Name), "sas9")>0 Then
newname=newname & " " & name
End If
Next

If InStr(LCase(newname), "sas 9")>0 Or InStr(LCase(newname), "sas9")>0 then
msgbox "Run V9"
sasflag="9"
Else msgbox "Run V8"
sasflag="8"
End If
....
....
 
Skip: There was no missing End If in the original code... The 3rd line If InStr(LCase(Name), ".sas") Then pgm = Name stands on it's own and does not require an "End If"

feipezi: Please explain in words what you are trying to accomplish.
 
[blush] ah, yes.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Hi,

I was trying to sweep the folder to see if there is a file named "run sas 9.txt" or "run sas9.txt", which tells the user to run one of the pgms in the folder under sas application 9; otherwise, run it under sas application 8. Then the VBS macro will start the application in version 9 or v8 depending on the results of the sweep, and run it. This is a part of the automation process. The next I'm going to work on is to let VBS check the sas Log to see if there is any error or warning message and so on. Right now, all I can do is to do manual checking by CNTL+F in Log window. We are programmers. Programmers usually hate manual work, any manual work. So the idea is to keep manual work minimum. Hope I made myself clear.

Thanks.
 
Something like this should work:
Code:
sasflag = "8"
For Each file in folder.Files
   name = LCase(file.Name)
   If InStr(LCase(Name), "sas 9") Or InStr(LCase(Name), "sas9") Then
      sasflag = "9"
      Exit For
   End If
Next
MsgBox "Run SAS " & sasflag
 
Just for laughs, I had a quick go at this in Powershell (because, VB is so last century! [wink]), and came up with...

Code:
$sasVersion="8"
if ( Get-ChildItem ".\" "run sas*.txt" | Where-Object {$_.Name -like "*9*"} )
    {
        $sasVersion="9"
    }
write-host "Run SAS $sasVersion"

But I'm not evangelising here, go with whatever rocks your world (and I appreciate that your original code snippet was part of a larger script). [smile]

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top