If statement not being entered VBSCript

I don't understand why but teh below VBScript is giving no output, the problem seems to be in the last if statement but I can't see anything wrong with it can anyone advise?

Dim objFSo, strDirToClean, deleteFilesOlderThanDays

If WScript.Arguments.Count = 2 Then
      Set args = WScript.Arguments
      strDirToClean = args.Item(0)      
      deleteFilesOlderThanDays = args.Item(1)
Else
      If strDirToClean.equals("/?") Then
            wscript.echo "Usage: directory to clean no of days to delete files older than"
      End If
End If


Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirToClean)
Set colFiles = objFolder.Files

For Each objFile in colFiles
      fileModifyDate = objFile.DateLastModified
      daysSinceModify = datediff("d", fileModifyDate, date)            'Delete file only if it is older than modify date specified

      'wscript.echo daysSinceModify & " " & deleteFilesOlderThanDays

      If daysSinceModify > deleteFilesOlderThanDays Then
            wscript.echo "Deleting file " & objFile.name & " Last modified " & daysSinceModify & " days ago"
      '      objFSO.DeleteFile(objFile)          'uncomment when we can be sure that the logic is right
      End If

Next
LVL 1
mark_667Asked:
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.

santhimurthydCommented:
Check with converting the valeu to Integer type and then compare

If CInt(daysSinceModify) > CInt(deleteFilesOlderThanDays) Then
  objFSO.DeleteFile(objFile)  
End if

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
What does the (commented out) echo tell you about the values?
0
Bill PrewCommented:
Perhaps no files met the age check, try running it with this extra debugging display and see what files it processes and what the dates and ages are.

Dim objFSo, strDirToClean, deleteFilesOlderThanDays

If WScript.Arguments.Count = 2 Then
      Set args = WScript.Arguments
      strDirToClean = args.Item(0)      
      deleteFilesOlderThanDays = args.Item(1)
Else
      If strDirToClean.equals("/?") Then
            wscript.echo "Usage: directory to clean no of days to delete files older than"
      End If
End If

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirToClean)
Set colFiles = objFolder.Files

For Each objFile in colFiles
      fileModifyDate = objFile.DateLastModified
      daysSinceModify = datediff("d", fileModifyDate, date)            'Delete file only if it is older than modify date specified
      Wscript.Echo "File:[" & objFile.Path & "], Date:[" & fileModifyDate & "], Age:[" & daysSinceModify & "]"

      'wscript.echo daysSinceModify & " " & deleteFilesOlderThanDays

      If daysSinceModify > deleteFilesOlderThanDays Then
            wscript.echo "Deleting file " & objFile.name & " Last modified " & daysSinceModify & " days ago"
      '      objFSO.DeleteFile(objFile)          'uncomment when we can be sure that the logic is right
      End If
Next

Open in new window

~bp
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

mark_667Author Commented:
Qlemo the output with the echo statement uncommented is:
131 7
128 7
127 7
6 7
6 7
So it should delete 3 files and leave 2 but teh echo statement inside the If never prints anything.
0
mark_667Author Commented:
Thanks, I wouldn't have found that answer any time soon, the code worked exactly as I wanted with that change, thanks.

131 7
Deleting file Daily Backup 20110819 1830.sql Last modified 131 days ago
128 7
Deleting file Daily Backup 20110822 1830.sql Last modified 128 days ago
127 7
Deleting file Daily Backup 20110823 1830.sql Last modified 127 days ago
6 7
6 7
0
Bill PrewCommented:
Just in the interest of completeness, I think you only needed to convert one value to an integer.  DateDiff() should already be returning an integer, so no need to convert that.  So rather than do the conversion on every compare, I would suggest putting:

If CInt(daysSinceModify) > CInt(deleteFilesOlderThanDays) Then

Open in new window

back to:

If daysSinceModify > deleteFilesOlderThanDays Then

Open in new window

and rather doing the one time conversion from the string parm to an integer as in:

deleteFilesOlderThanDays = CInt(args.Item(1))

Open in new window

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.