Solved

VBS - Delete Files in Directory.

Posted on 2006-10-20
8
20,525 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 146
can finally block executed even when there is a return statement in try block 4 85
Python Encoding Problem \u2013 4 135
SQL400 max size 5 106
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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

749 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