Solved

VBA delete all files in a Folder

Posted on 2013-06-27
2
477 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
[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
  • 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

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!

Question has a verified solution.

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

Outlook Free & Paid Tools
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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