Solved

VBA delete all files in a Folder

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

856 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