Delete folders

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a set of Windows folders named by date, yyyymmdd:

20041031
20041105
20041109
20041110

I want to delete all folders prior to today. I know how to turn the current
date into a string like those above and how to use the DeleteFolder method
once I select a folder, but how do I select the first folder whose date is
earlier than today? Once I have this, I can simply Do While...Loop until
there are no more that meet this criteria.
 
OK, I figured it out on my own Here is the code that worked. It generates a
data type mismatch error 13 on the DeleteFolder method.

Dim TestDir
Dim DirName
Dim DirPath
Dim FileSysObj
Dim DelFolder
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
TestDir= "C:\Test\" ' set the name of the parent directory
DateTemp = Format(Date, "yyyymmdd")
DirName = Dir(TestDir, vbDirectory) ' Retrieve the name of the first
subdirectory
Do While Len(DirName) > 0 ' Start the loop.
' Ignore the current directory and the encompassing directory.
If DirName <> "." And DirName <> ".." Then
' Use bitwise comparison to make sure DirName is a directory.
DirPath = TestDir & DirName
If (GetAttr(DirPath) And vbDirectory) = vbDirectory Then
If DirName < DateTemp Then 'if directory is older than date scope
Set DelFolder = FileSysObj.DeleteFolder(DirPath)
End If
End If
End If
DirName = Dir ' Get next directory name
Loop
 
Since you're deleting the folder, there's no object returned by the method.

Try changing

Set DelFolder = FileSysObj.DeleteFolder(DirPath)

to simply

FileSysObj.DeleteFolder(DirPath)

On the other hand, I don't see a need to invoke the overhead of FSO when VBA
is perfectly capable of deleting folders. The following should work just as
well:

If (GetAttr(DirPath) And vbDirectory) = vbDirectory Then
If DirName < DateTemp Then 'if directory is older than date scope
Kill DirPath & "\*.*
RmDir DirPath
End If
End If
 
So, now, how do I check to see if there are any files in that folder first
(i.e before Kill)? Othewise, I get a file not found error.
 
The reason I was using DeleteFolder was that it acts like DelTree rather than
Del, so that I do not have to check for downline files/folders. What if there
are downline folders? I can bypass the errors with On Error Resume Next, but
can VBA force deletion of the folder & all sub folders/files?
 
No, unfortunately VBA can't force deleting. Another option would be to use
the Shell statement to run DOS commands.

I think you could also use the ShFileOperation API. Randy Birch has an
example of its use at http://vbnet.mvps.org/code/shell/shfileopadv.htm but
I'm not 100% certain it'll clean folders that aren't empty. (Obligatory
warning: Randy's site is aimed at VB programmers. There are significant
differences between the controls available for VB forms and Access forms, so
sometimes his examples won't port directly. In this particular one, for
instance, he's using a control array, which Access doesn't support. However,
you should still be able to figure out how to call the API)

If you're happy with FSO, however, go for it. I just prefer not to.
 
So, now, how do I check to see if there are any files in that folder
first
(i.e before Kill)?


if len(dir(strMyFolder & application.PathSeparator & "*")) > 0 Then
' there's a file

else
' there wasn't

end if

Hope that helps

Tim F
 
Back
Top