laststock007
MIS
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
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