Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to empty folder on script execution...

Posted on 2004-09-01
9
Medium Priority
?
191 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
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!

 
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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 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