# Tons of If statements

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...
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT Security Analyst SeniorCommented:
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
Billing EngineerCommented:
I would alsohave the RTP xxx variable to be an array....
0
Commented:
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

Experts Exchange Solution brought to you by