Solved

VBS - Delete Files in Directory.

Posted on 2006-10-20
8
20,511 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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 73
matchUp  challenge 9 94
Python - ImportError: No module named 'urllib2' 2 154
java continue statement 10 115
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now