Solved

VBA delete all files in a Folder

Posted on 2013-06-27
2
471 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft Access 2010, using same Header 4 55
Macro 6 58
Create Excel formula on dynamic data 5 42
Excel Hyperlink Question 4 48
Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 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