Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA delete all files in a Folder

Posted on 2013-06-27
2
Medium Priority
?
479 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

721 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