Solved

VBS - Delete Files in Directory.

Posted on 2006-10-20
8
20,507 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You.
0
 
LVL 35

Expert Comment

by:mvidas
Comment Utility
Hi Jay,

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

Matt
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:dave_moats
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Fair enough :) Just wondering was all, thanks for responding!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to manage invalidate between two tvirtualstringtree in same form? 1 92
maxMirror challenge 10 87
countClumps  challenge 10 89
countX 22 68
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 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

6 Experts available now in Live!

Get 1:1 Help Now