?
Solved

VBS - Delete Files in Directory.

Posted on 2006-10-20
8
Medium Priority
?
20,535 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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 learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

762 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