Need to empty folder on script execution...

Hi,

I just need a simple script that when it is run it will EMPTY the contents of a folder, but not delete the folder itself. The folder is "C:\Documents and Settings\User Name\My Documents\temp". Thanks in advance.
wonderbread123Asked:
Who is Participating?
 
ShauliConnect With a Mentor Commented:
Set a reference to Microsoft Scripting Runtime, and..

Private Sub Command1_Click()
Call DeleteFolder("username")
End Sub

Public Sub DeleteFolder(ByVal sbUserName As String)
Dim FSO As FileSystemObject, tmpFolder As String
Set FSO = New FileSystemObject
tmpFolder = Replace("C:\Documents and Settings\User Name\My Documents\temp", "User Name", sbUserName) & "\"
FSO.DeleteFile tmpFolder & "*.*"
FSO.DeleteFolder tmpFolder & "\*"
End Sub

S
0
 
ShauliCommented:
Private Sub Command1_Click()
Call DeleteFolder("username")
End Sub

Public Sub DeleteFolder(ByVal sbUserName As String)
Dim delFile As String, tmpFolder As String
tmpFolder = Replace("C:\Documents and Settings\User Name\My Documents\temp", "User Name", sbUserName) & "\"
On Error Resume Next
delFile = Dir(tmpFolder & "*.*", vbDirectory)
    Do While Not delFile = ""
        Select Case delFile
            Case ".", ".."
                'do nothing
            Case Else
                Kill tmpFolder & delFile
        End Select
        delFile = Dir
    Loop
MsgBox "Done"
End Sub

S
0
 
wonderbread123Author Commented:
Ok, that works on the files, but it still leaves the folders.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
wonderbread123Author Commented:
The folders INSIDE that folder, that is...
0
 
ShauliCommented:
Do you want to remove the subfolders, or just empty them?

S
0
 
wonderbread123Author Commented:
I want to delete the subfolders.

Where do I set that reference?
0
 
ShauliCommented:
From the menu bar, click Project, select References, and scroll down to Microsoft Scripting Runtime, and check the checkbox on the left.

S
0
 
wonderbread123Author Commented:
It works perfectly, thanks alot!
0
 
ShauliCommented:
You are welcome, Thanks for the points :)

S
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.