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!

Script - How to delete certain number of files in a new folder

Status
Not open for further replies.
Sep 30, 2008
4
US
Hello everyone. I just joined Tek-Tips and this is my first post. I want someone with knowledge to tell me how to delete all files in a certain folder but only keep the first 100 or 200 files/logs?

The trick is, a new folder is created everyday to correspond to the day. Example, there is a new folder for September 30 today. Tomorrow, there will be another folder for October 1, etc., etc.,

Here is the bulk of the info you may need.


Current Script is...
Start in: C:\Logs\Queries\Processed
Run: C:\Logs\Queries\Processed\delete-problem-logs.vbs

SCRIPT:

'//******************************************************//
'//SCRIPT NAME :del_logs.vbs
'//AUTHOR :********
'//ENVIRONMENT :Windows 2000
'//DESCRIPTION : Delete log files
'//DATE : 09/29/2008
'//SYNTHAX :del_logs.vbs
'//DOCUMENTATION :
'// This script can be executed manually or via a scheduler.
'//CHANGES :
'//WHO WHEN WHAT
'//------------------------------------------------------
'//
'//******************************************************//
option explicit

'//We don't want the script to get stuck.
On Error Resume Next

Dim strDate
Dim logDir
Dim fso
Dim fsoSpec
Dim fsoFiles
Dim file

'//Instantiates the FileSytemObject
Set fso=CreateObject("Scripting.FileSystemObject")

'//Put the date in a string so it can be manipulated.
strDate = dateadd("d", 0, Date())

'//Obtain the name of the folder for the current day
logDir = "c:\logs\Queries\processed\" & DatePart("yyyy", strDate) & "\" & Right("0" & DatePart("m", strDate), 2) & "\" & Right("0" & DatePart("d", strDate), 2)

'//if the folder exists
If fso.FolderExists (logdir) Then
'//Instantiate the GetFolder and Files
set fsoSpec = fso.GetFolder(logDir)
Set fsoFiles = fsoSpec.Files

'//Only start removing files if the folder is not empty.
If fsoFiles.Count = 0 Then
'wscript.echo "The folder is empty. Doing nothing"
Else
'wscript.echo "The folder is not empty. Deleting files"
For each file in fsoFiles
'wscript.echo "deleting the " & file
fso.DeleteFile(file)

Next
End If
Else
'wscript.echo "The folder " & logDir & " doesn't exist. Doing nothing"

End if
 
I've taken the liberty of rewriting your script a bit. Is this what you want?

'//******************************************************//
'//SCRIPT NAME :del_logs.vbs
'//AUTHOR :********
'//ENVIRONMENT :Windows 2000
'//DESCRIPTION : Delete log files
'//DATE : 09/29/2008
'//SYNTHAX :del_logs.vbs
'//DOCUMENTATION :
'// This script can be executed manually or via a scheduler.
'//CHANGES :
'//WHO WHEN WHAT
'//------------------------------------------------------
'//
'//******************************************************//
option explicit

'//We don't want the script to get stuck.
On Error Resume Next

Dim strDate
Dim logDir
Dim fso
Dim fsoSpec
Dim fsoFiles
Dim file

'//Instantiates the FileSytemObject
Set fso=CreateObject("Scripting.FileSystemObject")

'//Put the date in a string so it can be manipulated.
strDate = dateadd("d", 0, Date())

'//Obtain the name of the folder for the current day
logDir = "c:\logs\Queries\processed\" & DatePart("yyyy", strDate) & "\" & Right("0" & DatePart("m", strDate), 2) & "\" & Right("0" & DatePart("d", strDate), 2)

'//if the folder exists
If fso.FolderExists (logdir) Then
'//Instantiate the GetFolder and Files
set fsoSpec = fso.GetFolder(logDir)
If fsoSpec.Files.Count = 0 Then
'wscript.echo "The folder is empty. Doing nothing"
Else
'wscript.echo "The folder is not empty. Deleting files"
'******************
KeepFiles fsoSpec, 200 ' keep the first 200 files in fsoSpec
End If
Else
'wscript.echo "The folder " & logDir & " doesn't exist. Doing nothing"

End If

Sub KeepFiles (ByVal objFolder, ByVal NumberOfFilesToKeep)

Dim i, j
Dim arrFiles
Dim objFile

i = 0
ReDim arrFiles (objFolder.Files.Count)
For Each objFile In objFolder.Files
i = i + 1
arrFiles (i) = objFile.Path
Next

For j = NumberOfFilesToKeep + 1 To i
objFSO.DeleteFile arrFiles (j), True
Next

End Sub
 
Oops, change the text "objFSO" to "fso". I use a different naming convention and didn't change that in the Sub. Sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top