Solved

VBA delete all files in a Folder

Posted on 2013-06-27
2
469 Views
Last Modified: 2013-06-28
I have a folder called P:\Pix
I want to delete every file in that folder

I am using the following code:

Dim dbs As Database
Dim fs, f, s, Folder

Set fs = CreateObject("Scripting.FileSystemObject")
Set Folder = fs.getfolder("P:\Pix")
Folder.Delete

I get an error message "Access Denied" if the folder is not empty.  

Question:  How do I delete every file in that folder?

Help is appreciated

Biggles
0
Comment
Question by:Biggles1
  • 2
2 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 39281403
Try this
OM Gang

Dim dbs As Database
Dim fs, f, s, Folder

Set fs = CreateObject("Scripting.FileSystemObject")
Set Folder = fs.getfolder("P:\Pix")
For Each f In Folder.Files
    f.Delete
Next
Folder.Delete
0
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 39281420
If you're doing this in VBA from an Office application you can set a reference to the
Microsoft Scripting Runtime
library and then utiize early binding and intellisence in your procedure.  Intellisense is a great assistant when writing code.
OM Gang

Dim dbs As Database
Dim fs As Scripting.FileSystemObject
Dim Folder As Scripting.Folder
Dim f As Scripting.File
Dim s

Set fs = CreateObject("Scripting.FileSystemObject")
Set Folder = fs.getfolder("P:\Pix")
For Each f In Folder.Files
    f.Delete
Next
Folder.Delete
0

Featured Post

What Security Threats Are You Missing?

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.

Join & Write a Comment

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

746 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

13 Experts available now in Live!

Get 1:1 Help Now