Solved

Need to empty folder on script execution...

Posted on 2004-09-01
9
178 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 71
VB6 Compile Compatibility Issue 4 121
Child Form in front 4 56
Remove Exif Orientation before adding image to db 9 38
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

735 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