| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Automatic file delete
Hello,
Is there a program that can automatically (schedule) delete files from a certain folder? based on the file creation date (e.g. delete all files that are older then 7 days)? or - is it easy enough to program a service to do so? 10x |
|
#2
|
|||
|
|||
|
Hello csillagyitzik,
Try to setup the Scheduled Tasks for the VBScript below. Hope it will help .WARNING!!!: Make sure to backup your source folder before testing. This script won't place your files into Recycle Bin. Since this script base on date created of the file, you should test to delete your files from its original location. If you copy your files to another location the date created will change to the current date then no file match with your criteria to delete. '-------------- CleanUp.vbs -------------------- Option Explicit Dim fso Set fso = CreateObject("Scripting.FileSystemObject") DeleteFiles fso.GetFolder("C:\Test") '<----- Change this folder name to match with your case. Sub DeleteFiles(srcFolder) Dim srcFile If srcFolder.Files.Count = 0 Then Wscript.Echo "No File to Delete" Exit Sub End If For Each srcFile in srcFolder.Files If DateDiff("d", Now, srcFile.DateCreated) < -7 Then fso.DeleteFile srcFile, True End If Next Wscript.Echo "Files Deleted successful" End Sub '----------------------------------------------- |
|
#3
|
|||
|
|||
|
delete files on hourly basis
Hi James
following script you put here is really good, and it works, but I have little problem of my own. that I need to delete log files on hourly basis instead of Day basis, I am Zero in VB. I thought with small changes like DateDiff > TimeDiff and DateCreated > TimeCreated< I might get it working. But it didn't work. Is there anyway I can make this script to delete files ina folder which are more than 2 hours old and send output to a text file. Your help will be much appreciated. Sincerely Jawed Abbasi Jabbasi@yahoo.com Quote:
|
|
#4
|
|||
|
|||
|
Hello Jabbasi,
Sorry for a late response. Just modify the DateDiff function as below to delete files in a folder which are more than 2 hours old. Code:
...
For Each srcFile in srcFolder.Files
If DateDiff("h", Now, srcFile.DateCreated) < -2 Then
fso.DeleteFile srcFile, True
End If
Next
...
Quote:
|
|
#5
|
|||
|
|||
|
Quote:
hiya pals, errr.. can i make a request on this? can you make this script silent delete, with no prompt at all? cuz i want to delete folders, cud i not delete the whole folder? thanks for the reply. -maester |
|
#6
|
|||
|
|||
|
Can this be modified to only delete files with a specific extension as well?
I.E. I have a bunch of *.txt files I would like to delete using this script. There are other files in the directory so a mass delete is unacceptable. Thank you in advance, J Quote:
|
|
#7
|
||||
|
||||
|
here is the modified script:
Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Call DeleteFiles(fso.GetFolder("C:\Test"), 7, ".txt") '<----- Change this folder name to match with your case.
Sub DeleteFiles(srcFolder, daysCount, strExtension)
Dim srcFile, filesCount
filesCount = 0
If srcFolder.Files.Count = 0 Then
Wscript.Echo "No File to Delete"
Exit Sub
End If
For Each srcFile in srcFolder.Files
If DateDiff("d", Now, srcFile.DateCreated) < ((-1)*daysCount) Then
If (strExtension="") Or (Right(UCase(srcFile.Name), Len(strExtension))=UCase(strExtension)) Then
fso.DeleteFile srcFile, True
filesCount = filesCount+1
End If
End If
Next
Wscript.Echo filesCount & " Files Deleted successfully"
End Sub
|
|
#8
|
||||
|
||||
|
Quote:
|
|
#9
|
|||
|
|||
|
uhm, one final request/question? how will i make this script repeat itself, say, every 15 days, or 30 days? i just want to delete a whole folder, with spaces in their names. (without the script counting the files in it..)
thanks Wizard! : ) |
|
#10
|
||||
|
||||
|
via the Schedules Task. in the Scheduled Task Wizard, when adding task or
editing one, you can set such details. |
|
#11
|
|||
|
|||
|
Great script -- what a lifesaver!
Sorry to pile on, but I wonder if I could ask about one more modification: Is it possible to have the script delete all files in the directory, regardless of how old they are? I tried modifying the file but couldn't get the syntax right. I realize I can change the DateDiff function to use -1 seconds, but was just curious if there's an even cleaner approach. Thanks much, Marv |
|
#12
|
||||
|
||||
|
using the modified code I posted, you can simply give very big number:
Code:
Call DeleteFiles(fso.GetFolder("C:\Test"), -999999, "")
this will delete all files in the folder with minimal change in the code. |
|
#13
|
|||
|
|||
|
Quote:
ahm, what about passwords? will it still run without a proper password? sorry if i am such a dumb-er newb.. : ( |
|
#14
|
||
|
|