Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File deletion from specific folders runs with no errors but doesnt delete. 1

Status
Not open for further replies.

Andym8674

Technical User
May 7, 2014
5
0
0
GB
hello,

the below script runs and doesn't output any errors but its doesn't delete the files. just wondering if anyone could help with this please :)



dim fso, fldr, x

set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder("C:\FTPdata")
for each x in fldr.files
if datediff("d", x.datelastmodified, now()) > 15 and _
right(x.path, 4) = ".DAT" then
fso.deletefile x.path
End if

Next


set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder("C:\mobileiron")
for each x in fldr.files
if datediff("d", x.datelastmodified, now()) > 2 and _
right(x.path, 4) = ".tgz" then
fso.deletefile x.path
End if

Next

set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder("C:\block\PRIME")
for each x in fldr.files
if datediff("d", x.datelastmodified, now()) > 2 and _
right(x.path, 4) = ".gpg" then
fso.deletefile x.path
End if

Next

set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder("C:\block\ISE")
for each x in fldr.files
if datediff("d", x.datelastmodified, now()) > 2 and _
right(x.path, 4) = ".gpg" then
fso.deletefile x.path
End if

Next
 
Here is a cleaner way to do this.
Code:
Call DeleteFiles("C:\FTPdata",15,"dat")
Call DeleteFiles("C:\mobileiron",2,"tgz")
Call DeleteFiles("C:\block\PRIME",2,"gpg")
Call DeleteFiles("C:\block\ISE",2,"gpg")

Sub DeleteFiles(Path,Ext,n)
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	For Each File In oFSO.GetFolder(Path).Files
		If oFSO.GetExtensionName(File) = Ext And DateDiff("d",File.DateLastModified,Now) > n Then
			File.Delete
		End If 
	Next
End Sub
 
PLEASE disregard my previous post and use this.
Code:
Call DeleteFiles("C:\FTPdata","dat",15)
Call DeleteFiles("C:\mobileiron","tgz",2)
Call DeleteFiles("C:\block\PRIME","gpg",2)
Call DeleteFiles("C:\block\ISE","gpg",2)

Sub DeleteFiles(Path,Ext,n)
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	For Each File In oFSO.GetFolder(Path).Files
		If oFSO.GetExtensionName(File) = Ext And DateDiff("d",File.DateLastModified,Now) > n Then
			File.Delete
		End If 
	Next
End Sub
 
thanks this really is a lot cleaner :)

just out of interest is there a wild card that can be used for all subfolders for a specific folder and also a wild card for all extension?

c:\ftpdata\all subfolders and all extensions?

thanks again :)
 
You're welcome. In regards to your follow-up question, there are no wildcards for what you are trying to accomplish. Can you give an example of what you are trying to accomplish with going through each subfolder and files. Take care.
 
hello Jk speed, sorry for the late response, no problem your original infor has worked fantstically, i really appreciate your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top