Solved

Need to empty folder on script execution...

Posted on 2004-09-01
9
167 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
Comment Utility
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
Comment Utility
Ok, that works on the files, but it still leaves the folders.
0
 

Author Comment

by:wonderbread123
Comment Utility
The folders INSIDE that folder, that is...
0
 
LVL 19

Expert Comment

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

S
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Accepted Solution

by:
Shauli earned 125 total points
Comment Utility
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
Comment Utility
I want to delete the subfolders.

Where do I set that reference?
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
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
Comment Utility
It works perfectly, thanks alot!
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
You are welcome, Thanks for the points :)

S
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now