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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to auto delete files 1

Status
Not open for further replies.

disturbedone

Vendor
Sep 28, 2006
781
AU
Hi, I'm a complete newbie with scripting but I found a script online that will auto delete files that are older than 7 days but it's only 99% of what I need.

The scenario is I have an FTP server that I only want to keep files for 7 days, allowing users to download within that time and then they automatically get deleted. The script has the dateadd command but I've found that this looks at the Modified Date that Windows applies, not the Create Date. For example, a file from years ago that is copied into the FTP directory is allocated a new Create Date but the Modified Date is still years old so when the script runs at night it is deleted on it's first day.

Is there another command or any other way of getting a script to look at the correct date?

Any assistance is appreciated.

Cheers
 
Is there another command
another than WHAT ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
As my post stated, there is a command (whether you want to call it that or not I don't know) called 'dateadd'. This is used to determine the date that the file was added to the folder and what the current date is to decide if it should be deleted or not. As I stated, this "command" looks at the Modified Date, not the Created Date. I asked if there is another "command" that can be used to do such a thing. Do you know of a way to do this?

Thx
 
If it's useful, below is the script I currently use.

' Objective: To delete old files from a given folder and all subfolders below
' Created by: MAK
' Created Date: June 21, 2005
' Usage: cscript deloldfiles.vbs c:\dba\log 3
' : It deletes files older than 3 days
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
wscript.echo ""
wscript.echo "Deleting Files under the Folder:" & folders.path
wscript.echo "__________________________________________________________________________"
for each file in files
if file.datelastmodified < newdate then
wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
on error resume next
file.delete
end if

next

for each folder in subfolders
recurse folder
next

set subfolders = nothing
set files = nothing

end sub
 
Replace this:
file.datelastmodified
with this:
file.DateCreated

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top