Jump to content

Recommended Posts

Posted (edited)

Hi Guys Im new to this forum and just started my new job, I have been working on abit of VBS scribt to delete files older than 2 days in a shared directory. It has to remove all files and folder in 1 particular directory called C:\share48. Problem being is that I get a dialog box which I dont want popping up. Secondly it also removes the shre48 folder which i dont want it to, its supposed to delete all files and folders below it.

 

This is what i came up with:

 

_______________________________________

 

Dim oFSO, oFolder, oFiles, oFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("c:\share48\test")

Set oFiles = oFolder.Files

for each oFile in oFiles

if oFile.DateLastModified > DateAdd("d", 2, Now()) then

 

oFile.Delete True

end if

next

 

 

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _

("Select * from Win32_Directory where Name = 'c:\\share48'")

For Each objFolder in colFolders

errResults = objFolder.Delete

Wscript.Echo errResults

 

next

 

or use this script but it also deleted the c:\share48 folder which I dont whant it to.

 

strPath = "C:\share48"

 

DeleteFolder strPath

 

Function DeleteFolder(strFolderPath)

Dim objFSO, objFolder

Set objFSO = CreateObject ("Scripting.FileSystemObject")

If objFSO.FolderExists(strFolderPath) Then

objFSO.DeleteFolder strFolderPath, True

End If

Set objFSO = Nothing

End Function

 

 

___________________________________________________________

 

Please Help ^^

 

cheers guys.

Edited by RobPrince
Posted

You seem to have got several scripts mixed up. the first chunk should work just fine although your dateadd is the wrong way round ("old" dates are smaller numbers)

 

The message box popping up is probably because you're running this with wscript instead of cscript - either type "cscript nameofscript.vbs" or change to use cscript as default (cscript //h:cscript)

 

Dim oFSO, oFolder, oFiles, oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("c:\share48\test")
Set oFiles = oFolder.Files
for each oFile in oFiles
if oFile.DateLastModified < DateAdd("d", -2, Now()) then

oFile.Delete True
end if
next

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...