-3

I need some help on below script I am doing a auto delete based on file extension and date modified.

I just started on vbscript so not really sure about how it worked.

I will want it to delete older file based on date and file extension when the first condition is true and if the condition is false it will end

Option Explicit
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objSTR, objTYP, objFILE`enter code here`
Dim Folder, SubFolder

Dim objNetwork, strRemoteshare, strUser, strPassword
Set objNetwork = WScript.CreateObject("WScript.Network")
strRemoteShare = "shared drive path"
strUser = "user"
strPassword = "password"
objNetwork.MapNetworkDrive "X:", strRemoteshare, True, strUser, 
strPassword
objNetwork.RemoveNetworkDrive "X:", True , True 

objTYP = "file extension"
For Each objFILE In objFSO.GetFolder("folder path").Files
If objFile.Type = objTYP And objFILE.DateLastModified < 
DateAdd("d",-5,Now) = True Then
'in the statement here i want it delete the older file based on  
'objFile.Type = objTYP And objFILE.DateLastModified > DateAdd("d",-10,Now)
ElseIf objFile.Type = objTYP And objFILE.DateLastModified < 
DateAdd("d",-5,Now) = False Then
End If
Next 
Kai jie
  • 1
  • 2
  • Probably, but your code is unindented and includes many lines that do nothing. – user14797724 Feb 22 '21 at 08:00
  • 1
    `If` statement conditions need to evaluate to `True`. In your `If` the condition `objFILE.DateLastModified < DateAdd("d",-5,Now) = True` makes no sense try just `objFILE.DateLastModified < DateAdd("d",-5,Now)` instead which will evaluate to `True` if the `DateLastModified` is less then five days from todays date and time. You can then remove the `ElseIf` line completely and replace it with just `Else` which will evaluate the `False` outcome. – user692942 Feb 22 '21 at 09:15
  • I am doing this for backup and I will want it to delete older backup after it detect that new backup have been done. so if I put condition as `objFILE.DateLastModified < DateAdd("d",-5,Now)` is `True` and I want the statement to delete files that are more then 7 days from todays date and time. what will I need to put? thanks for the help. – Kai jie Feb 22 '21 at 10:02
  • @Kaijie you would need to change the `-5` to `-7` in the `DateAdd()` as that represents the previous number of days to compare against today's date. Then you would want to call the [`Delete()` method of the `File` object](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/0k4wket3(v=vs.84)) which you have assigned to `objFILE`. – user692942 Feb 22 '21 at 10:06

1 Answers1

0

Based on the comments in the question you need to first fix the evaluation and then add the call to File object Delete() method.

For Each objFILE In objFSO.GetFolder("folder path").Files
  If objFILE.Type = objTYP And objFILE.DateLastModified < DateAdd("d", -7, Now()) Then
    'File older than 7 days will be removed.
    Call objFILE.Delete(True) 'Pass true to force deletion.
  End If
Next

Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
  • The file that I want to detect and delete is Veeam full backup file. I tried the command and is unable to delete I change it and it is able to delete TXT file but not the backup. the folder path is in a shared drive does it matter? so sorry about the command not really sure how to do it. `For Each objFILE In objFSO.GetFolder("Folder path").Files` `If objFILE.DateLastModified < DateAdd("d", -7, Now()) Then` `'File older than 7 days will be removed.` `Call objFILE.Delete(True) 'Pass true to force deletion.` `End If` `Next` – Kai jie Feb 23 '21 at 02:10
  • You may need to call the script with elevated permissions. You may also need to check the permissions assigned to the shared drive, do you get a specific error when try the script? – user692942 Feb 23 '21 at 02:20
  • Before that I was using batch file and it is able to delete the file but unable to detect is there new backup. I am using vbsedit to run it and I get no error ***** script completed - exit code: 0 ***** – Kai jie Feb 23 '21 at 02:33
  • If I remove `objFILE.Type = objTYP` and add in `WScript.Echo "check"` I will have a output but when I add in it doesn't show. I think that the problem might be the file extension as the file extension is .vbk which is also a audio file extension. for the `objTYP = "file extension"` i put the full name of the extension not sure it will work – Kai jie Feb 23 '21 at 04:33
  • @Kaijie sounds like the issue is with your code, I can only provide an answer based on the information available to me which has been provided by you. If the `objTYP` variable isn't doing what you expect that has no bearing on the initial problem of your evaluation being incorrect, which this answer has covered. – user692942 May 13 '21 at 00:21