How to check for the existence of the Temporary folder namedTemporary Folder a , 2 and so on before

Y

Yuvraj

Hi All,

I am using the following code


Set FSO = CreateObject("scripting.filesystemobject")
FSO.DeleteFolder Environ("Temp") & "\Temporary Directory*", True

The purpose is that I want to delete the Folder NAmed as Temporay
Directory a and Temporary Directory b.

I want to check the existence of the folder before deleting.

I am wondering how to do it as the name is not constant.

What is constant is Temporary Dirctory and then variable names

I also do not want to delete all the folders.

Hence can some one provide some guidance.

Regards,

Yuvraj
 
B

Barb Reinhardt

This should get you started.

Option Explicit

Sub test()
Dim FSO As Object
Dim myParentFolder As Object
Dim myChildFolder As Object
Dim Verify As VbMsgBoxResult
Dim FoundFolder As Boolean

Set FSO = CreateObject("scripting.filesystemobject")

Set myParentFolder = FSO.Getfolder(Environ("temp"))
Do
FoundFolder = False
For Each myChildFolder In myParentFolder.Subfolders
Debug.Print myChildFolder.Name
If myChildFolder.Name Like "*Temporary Directory*" Then
Verify = MsgBox("Do you want to delete folder: " &
myChildFolder.Name & "?", vbYesNo)
If Verify = vbYes Then
myChildFolder.Delete
FoundFolder = True
Exit For
End If
End If
Next myChildFolder
Loop While Verify = vbYes
End Sub


HTH,
Barb Reinhardt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top