?
Solved

VBS - Delete Files in Directory.

Posted on 2006-10-20
8
Medium Priority
?
20,545 Views
Last Modified: 2011-08-18
I need to modify this script, instead of it deleting files that are older than a day, to if there is more that 1 file in the directory to delete all the files in the backup directory.

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
'Delete database log file backups 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 log file backups 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
0
Comment
Question by:jay_eire
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 17774066
Hi Jay,

Unrelated to the question at hand, do you realize your code above is doing the same thing twice?

As for this question:
Option Explicit
On Error Resume Next
Dim oFSO
Dim sDirectoryPath
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\"
If oFSO.GetFolder(sDirectoryPath).Files.Count > 1 Then oFSO.DeleteFile sDirectoryPath & "*.*", True
Set oFSO = Nothing
Set sDirectoryPath = Nothing

Matt
0
 
LVL 6

Accepted Solution

by:
dave_moats earned 2000 total points
ID: 17787992
To get a count of the files in a directory, you will want to use the file collection .count method - oFolder.Files.count.

I took your code and modified it to use the files.count method, also I removed the second delete loop, moved a couple of the sets from the initail cleanup to inside the if statement, and I changed the order of the clean up calls in general.  It is always a good idea to destroy object in the reverse order you create them, that way you are always sure that any dependencies are cleanedup properly.

[ Start Code ]

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 > 1 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

[ End Code ]
0
 
LVL 7

Author Comment

by:jay_eire
ID: 17793959
Thank You.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:mvidas
ID: 17794761
Hi Jay,

For my curiosity and for me helping future people with similar questions, what was wrong with my code above?

Matt
0
 
LVL 6

Expert Comment

by:dave_moats
ID: 17794971
Matt,

I want to apologize, when I saw your answer, I just glanced at it and saw the "your code does the same thing twice" and did not actually read the rest of the response.   Because I did not read it, I did not realise you had already answered the question properly.

Jay might have done the same thing, but I do not know for sure.

Again, I apologize for my oversight.

Dave
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17795022
Dave,

No worries on my end, I don't care about points and you did explain to him how the property worked, so I'm fine with how it was closed.  I was just curious if the *.* hadn't worked for him, didn't know if his setup could have blocked that usage or something.

Matt
0
 
LVL 7

Author Comment

by:jay_eire
ID: 17795182
Apologies Matt, I seen "Unrelated to the question at hand" and  I thought you had copied and pasted the repeated lines of the original code I posted up to show, to show I only needed to use it once.
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17795218
Fair enough :) Just wondering was all, thanks for responding!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 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