Solved

VBS Script - Delete files/check date of files

Posted on 2006-10-26
1
7,370 Views
Last Modified: 2011-10-03
Hi Guys,
I need some help combining these two scripts, the first script deletes backups that are a day old, the second script checks to see if there is more than two backups in the folder and if there is deletes everything in the backup folder.

what i need the script to do is....

Check to see if there is more than two files in the backup directory, if there is then delete the older backup of the two, and keep the newer backup....


here is the two scripts................

Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
'Delete database backup files older than 1 days
iDaysOld = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'If database backup files are older than 1 days, delete them.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing


Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile

'Delete delete all files in the directory if more than one file exists

Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\"
set oFolder = oFSO.GetFolder(sDirectoryPath)

if oFolder.Files.count > 0 then
   set oFileCollection = oFolder.Files

   For each oFile in oFileCollection
      oFile.Delete(True)
   Next

   ' clean up
   Set oFile = Nothing
   Set oFileCollection = Nothing
End If

'Clean up
Set oFolder = Nothing
Set oFSO = Nothing





0
Comment
Question by:jay_eire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
mattcarver earned 500 total points
ID: 17834714
>>what i need the script to do is....
>>Check to see if there is more than two files in the backup directory, if there is then delete the older backup of the two, and >>keep the newer backup....

You said you need the script to see if there are "more than two files" but I think you meant "two" file or "two or more".  If that is the case you can use the following script.

The following script will delete the oldest file in the directory but will only execute if there are 2 or more files in the directory.  This script does not check too see if the backup files are older than one day.  You didn't say you wanted that.

--------- cut and paste into a filename with a .vbs extension.  double-click to execute -----------
Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim CompDate

'Delete delete all files in the directory if more than one file exists
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\"
Set oFolder = oFSO.GetFolder(sDirectoryPath)
CompDate = Null

if oFolder.Files.count > 1 then
   set oFileCollection = oFolder.Files

   'Find the date of the oldest file
   For each oFile in oFileCollection
      If CDate(CompDate) > CDate(oFile.DateLastModified) Then
        CompDate = oFile.DateLastModified
      End If
   Next

   'Delete the file associated with the oldest date
   For each oFile in oFileCollection
      If CDate(oFile.DateLastModified) = CompDate Then oFile.Delete(True)
   Next

   ' clean up
   Set oFile = Nothing
   Set oFileCollection = Nothing
End If

'Clean up
Set oFolder = Nothing
Set oFSO = Nothing
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question