Solved

# Tons of If statements

Posted on 2013-10-05
Medium Priority
311 Views
I have to create if statements for 117 files that are being reviewed.  I wonder if there is a more consise way to write this:
The age of the reports changes.  RPT 1-3 are 60 mins, RPT 4-63 are 120 mins...

For Each file in f.Files
age = DateDiff("n", file.DateLastModified, Now)
'60 Minute Reports
If ((age > 60) and (lcase(right(file.name,4) = ".enc")) and InStr(1,file.name, RPT1,1) = 1) Then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file & vbCr
Elseif ((age > 60) and (lcase(right(file.name,4) = ".enc")) and InStr(1,file.name, RPT2,1) = 1) Then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file & vbCr
Elseif ((age > 60) and (lcase(right(file.name,4) = ".enc")) and InStr(1,file.name, RPT3,1) = 1) Then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file & vbCr
'120 Minute Reports
Elseif ((age > 120) and (lcase(right(file.name,4) = ".enc")) and InStr(1,file.name, RPT4,1) = 1) Then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file & vbCr
...END STATEMENT hundereds of lines later...
0
Question by:williamss132

LVL 17

Assisted Solution

Kent Dyer earned 400 total points
ID: 39549307
First thing you want to do is get rid of the tons of if statements and use SELECT...CASE..

http://www.w3schools.com/vbscript/vbscript_conditionals.asp

Other thing you can do is leverage the file system..

``````Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\dev"

Set objFolder = objFSO.GetFolder(objStartFolder)

For Each file in f.Files
If objFile.Extension = "enc" Then
..  other code
``````

If you are dealing with RPT1 through RPT117.  Setup a counter..

``````Dim i
FOR i=0 to 117
Wscript.echo i
Next
``````

I would then build your code around the counter..
``````SELECT CASE AGE
If ((age > 60) and InStr(1,file.name, RPT & i,1) = 1) Then
..do stuff
End IF
CASE..
``````

Hope this helps.
0

LVL 143

Assisted Solution

Guy Hengel [angelIII / a3] earned 400 total points
ID: 39549566
I would alsohave the RTP xxx variable to be an array....
0

LVL 33

Accepted Solution

Robberbaron (robr) earned 1200 total points
ID: 39550172
putting it al together with some more logic... separate by extn, then age , then check filename matches.

``````Dim RPT(63)   'the 63 report names
RPT(1)="my test-1"
...
RPT(63)="my last testfile"

for each file in f.Files

age = DateDiff("n", file.DateLastModified, Now)
extn = Lcase(f.Extension)
old_file_found = 0
old_file_name = ""
select case extn
case "enc"

select case age
case 1 to 60
'60min or less
'check the known 60min reports
for i=1 to 3
if InStr(1,file.name, RPT(i),1)=1 then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file.name & vbCr
exit for
end if
next i
case 61 to 120
'120min or less
'check the known 60min reports
for i=4 to 63
if InStr(1,file.name, RPT(i),1)=1 then
old_file_found = 1
old_file_name = old_file_name & vbCrlf & file.name & vbCr
exit for
end if
next i
case else
'greater than 120
' ? ? ?

case else
'unknown file extension
'do nothing for now
end select
next file
``````
0

Author Closing Comment

ID: 39552406
Thanks Experts!!
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.