SoftTree Technologies SoftTree Technologies
Technical Support Forums
RegisterSearchFAQMemberlistUsergroupsLog in
Deleting old files

 
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite View previous topic
View next topic
Deleting old files
Author Message
Johann Horsmeijer



Joined: 12 May 2003
Posts: 30

Post Deleting old files Reply with quote


I would like to use a script to delete files older then 30 days.
I downloaded this script but it does not seem to work.

Option Explicit
On Error Resume Next
'Deletes old files based on age. You must specify a target
'directory and max age. This script looks for these items
'on the command line - The directory is the first argument,
'and the max age is the second argument. If no arguments are
'supplied, the environment is Checked for KILL_FILES_IN and
'MAX_FILE_AGE. If no environment variables are found, the user
'is asked.

Main
Wscript.Quit 0

Sub Main()
Dim wsh 'As WScript.Shell
Dim fil 'As Scripting.File
Dim fils 'As Scripting.Files
Dim fol 'As Scripting.Folder
Dim fols 'As Scripting.Folders
Dim fs 'As Scripting.FileSystemObject
Dim strStartingDirectory 'As String
Dim strDirectories() 'As String
Dim lngCounter 'As Long
Dim dblMaxAge 'As Double

Const READONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4

'Create needed objects

Set fs = CreateObject("Scripting.FileSystemObject")

Set wsh = CreateObject("WScript.Shell")

'Initialize input variables

strStartingDirectory = "e:\werk"

dblMaxAge = 7

'Get data from command line

If Wscript.Arguments.Count > 0 Then

strStartingDirectory = Wscript.Arguments(0)

If Wscript.Arguments.Count = 2 Then

dblMaxAge = CDbl(Wscript.Arguments(1))

End If

End If

'Get data from the environment

If strStartingDirectory = "" Then

strStartingDirectory = wsh.Environment.Item("KILL_FILES_IN")

End If

If dblMaxAge = 0 Then

If IsNumeric(wsh.Environment.Item("MAX_FILE_AGE")) Then

dblMaxAge = CDbl(wsh.Environment.Item("MAX_FILE_AGE"))

End If

End If

'Ask user for data

If strStartingDirectory = "" Then

strStartingDirectory = InputBox("Enter path to start deleting at:", "Delete Old Files", FileNameInThisDir(""))

MsgBox "You could have supplied the path information as the environment variable KILL_FILES_IN or as the first argument to this script"

End If

If dblMaxAge = 0 Then

dblMaxAge = CDbl(InputBox("Enter the minimum time since file creation (in days) of files to delete", "Delete Old Files", "180"))

MsgBox "You could have supplied the file age information in the environment variable MAX_FILE_AGE or as the second argument to this script"

End If

'Test input data

Redim strDirectories(0)

strDirectories(0) = fs.GetAbsolutePathName(strStartingDirectory)

If strDirectories(0) = "" Then Wscript.Quit 1

If dblMaxAge Ubound(strDirectories,1)

'Next folder to process

Set fol = fs.GetFolder(strDirectories(lngCounter))

'Get each file in turn

Set fils = fol.Files

If Err.Number 0 Then Exit Sub

For Each fil In fils

If (CDbl(Now) - CDbl(fil.DateCreated)) > dblMaxAge Then

If ((fil.Attributes And READONLY) = 0) Then

If ((fil.Attributes And SYSTEM) = 0) Then

If ((fil.Attributes And HIDDEN) = 0) Then

If Lcase(fil.Path) Lcase(Wscript.ScriptFullName) Then

fil.Delete

End If

End If

End If

End If

End If

Next

'Check for any sub folders and add them to the folder array

Set fols = fol.SubFolders

For each fol in fols

If Lcase(fol.Name) "recycled" Then

Redim Preserve strDirectories(Ubound(strDirectories,1) + 1)

strDirectories(Ubound(strDirectories,1)) = fol.Path

End If

Next

lngCounter = lngCounter + 1

Loop
End Sub

Function FileNameInThisDir(strFileName) 'As String
'Returns the complete path and file name to a file in
'the script directory. For example, "trans.log" might
'return "C:\Program Files\Scripts\Database\trans.log"
'if the script was in the "C:\Program Files\Scripts\Database"
'directory.
Dim fs 'As Scripting.FileSystemObject

Set fs = CreateObject("Scripting.FileSystemObject")

FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName))

''''''''''Clean up

Set fs = Nothing
End Function

Tue Apr 20, 2004 10:06 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7966

Post Re: Deleting old files Reply with quote

It is very simple using FileDir utility. See http://www.softtreetech.com/24x7/archive/46.htm for details.
FileDir can be scheduled as a program type job.

For other free automation utilities and tips see http://www.softtreetech.com/24x7/script.htm

: I would like to use a script to delete files older then 30 days.
: I downloaded this script but it does not seem to work.

: Option Explicit
: On Error Resume Next
: 'Deletes old files based on age. You must specify a target
: 'directory and max age. This script looks for these items
: 'on the command line - The directory is the first argument,
: 'and the max age is the second argument. If no arguments are
: 'supplied, the environment is Checked for KILL_FILES_IN and
: 'MAX_FILE_AGE. If no environment variables are found, the user
: 'is asked.

: Main
: Wscript.Quit 0

: Sub Main()
: Dim wsh 'As WScript.Shell
: Dim fil 'As Scripting.File
: Dim fils 'As Scripting.Files
: Dim fol 'As Scripting.Folder
: Dim fols 'As Scripting.Folders
: Dim fs 'As Scripting.FileSystemObject
: Dim strStartingDirectory 'As String
: Dim strDirectories() 'As String
: Dim lngCounter 'As Long
: Dim dblMaxAge 'As Double

: Const READONLY = 1
: Const HIDDEN = 2
: Const SYSTEM = 4

: 'Create needed objects

: Set fs = CreateObject("Scripting.FileSystemObject")

: Set wsh = CreateObject("WScript.Shell")

: 'Initialize input variables

: strStartingDirectory = "e:\werk"

: dblMaxAge = 7

: 'Get data from command line

: If Wscript.Arguments.Count > 0 Then

: strStartingDirectory = Wscript.Arguments(0)

: If Wscript.Arguments.Count = 2 Then

: dblMaxAge = CDbl(Wscript.Arguments(1))

: End If

: End If

: 'Get data from the environment

: If strStartingDirectory = "" Then

: strStartingDirectory = wsh.Environment.Item("KILL_FILES_IN")

: End If

: If dblMaxAge = 0 Then

: If IsNumeric(wsh.Environment.Item("MAX_FILE_AGE")) Then

: dblMaxAge = CDbl(wsh.Environment.Item("MAX_FILE_AGE"))

: End If

: End If

: 'Ask user for data

: If strStartingDirectory = "" Then

: strStartingDirectory = InputBox("Enter path to start deleting at:",
: "Delete Old Files", FileNameInThisDir(""))

: MsgBox "You could have supplied the path information as the environment
: variable KILL_FILES_IN or as the first argument to this script"

: End If

: If dblMaxAge = 0 Then

: dblMaxAge = CDbl(InputBox("Enter the minimum time since file creation
: (in days) of files to delete", "Delete Old Files",
: "180"))

: MsgBox "You could have supplied the file age information in the
: environment variable MAX_FILE_AGE or as the second argument to this
: script"

: End If

: 'Test input data

: Redim strDirectories(0)

: strDirectories(0) = fs.GetAbsolutePathName(strStartingDirectory)

: If strDirectories(0) = "" Then Wscript.Quit 1

: If dblMaxAge Ubound(strDirectories,1)

: 'Next folder to process

: Set fol = fs.GetFolder(strDirectories(lngCounter))

: 'Get each file in turn

: Set fils = fol.Files

: If Err.Number 0 Then Exit Sub

: For Each fil In fils

: If (CDbl(Now) - CDbl(fil.DateCreated)) > dblMaxAge Then

: If ((fil.Attributes And READONLY) = 0) Then

: If ((fil.Attributes And SYSTEM) = 0) Then

: If ((fil.Attributes And HIDDEN) = 0) Then

: If Lcase(fil.Path) Lcase(Wscript.ScriptFullName) Then

: fil.Delete

: End If

: End If

: End If

: End If

: End If

: Next

: 'Check for any sub folders and add them to the folder array

: Set fols = fol.SubFolders

: For each fol in fols

: If Lcase(fol.Name) "recycled" Then

: Redim Preserve strDirectories(Ubound(strDirectories,1) + 1)

: strDirectories(Ubound(strDirectories,1)) = fol.Path

: End If

: Next

: lngCounter = lngCounter + 1

: Loop
: End Sub

: Function FileNameInThisDir(strFileName) 'As String
: 'Returns the complete path and file name to a file in
: 'the script directory. For example, "trans.log" might
: 'return "C:\Program Files\Scripts\Database\trans.log"
: 'if the script was in the "C:\Program Files\Scripts\Database"
: 'directory.
: Dim fs 'As Scripting.FileSystemObject

: Set fs = CreateObject("Scripting.FileSystemObject")

: FileNameInThisDir =
: fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName,
: "..\" & strFileName))

: ''''''''''Clean up

: Set fs = Nothing
: End Function

Tue Apr 20, 2004 10:41 am View user's profile Send private message
John L.



Joined: 21 Apr 2004
Posts: 2

Post Re: Deleting old files Reply with quote

You can also use FORFILES from the Win2k Resource Kit.
(Microsoft Support Site too)

Then you only need a 1 line DOS batch file.

FORFILES -s -p"C:\dirname" -m*.bak -d-30 -c"cmd /c echo @PATH\@FILE & del @FILE" >>logfile.log


Wed Apr 21, 2004 3:19 pm View user's profile Send private message
Display posts from previous:    
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite All times are GMT - 4 Hours
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


 

 

Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.