Solved

Need to empty folder on script execution...

Posted on 2004-09-01
9
184 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:wonderbread123
[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
  • 5
  • 4
9 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 11959043
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
 

Author Comment

by:wonderbread123
ID: 11959146
Ok, that works on the files, but it still leaves the folders.
0
 

Author Comment

by:wonderbread123
ID: 11959149
The folders INSIDE that folder, that is...
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 19

Expert Comment

by:Shauli
ID: 11959161
Do you want to remove the subfolders, or just empty them?

S
0
 
LVL 19

Accepted Solution

by:
Shauli earned 125 total points
ID: 11959245
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
 

Author Comment

by:wonderbread123
ID: 11959379
I want to delete the subfolders.

Where do I set that reference?
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11959414
From the menu bar, click Project, select References, and scroll down to Microsoft Scripting Runtime, and check the checkbox on the left.

S
0
 

Author Comment

by:wonderbread123
ID: 11959464
It works perfectly, thanks alot!
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11959491
You are welcome, Thanks for the points :)

S
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

627 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