×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to fix the IF/THEN statements?

How to fix the IF/THEN statements?

How to fix the IF/THEN statements?

(OP)
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.

RE: How to fix the IF/THEN statements?

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
'MISSING End If SOMEWHERE
'msgbox name & " " & pgm
Next
....
.... 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: How to fix the IF/THEN statements?

(OP)
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
....
....

RE: How to fix the IF/THEN statements?

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.

RE: How to fix the IF/THEN statements?

blush ah, yes.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: How to fix the IF/THEN statements?

(OP)
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.

RE: How to fix the IF/THEN statements?

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 

RE: How to fix the IF/THEN statements?

Just for laughs, I had a quick go at this in Powershell (because, VB is so last century! wink), and came up with...

CODE --> Century)

$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
Variables won't. Constants aren't
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close