Solved

delete all files from folder even if folder is blank

Posted on 2014-03-18
3
503 Views
Last Modified: 2014-03-23
hey guys,

i'm using this code to delete all files from the folder

Kill CurrentProject.Path & "\Working Folder\*"

Open in new window


however when there are no files in the folder it gives me this error message

Run-time error '53':

File not found.


Question: what is the code i should use to delete all files in a folder, regardless if there are files or not?

Thoughts: i'm thinking just put a On Error Resume Next and an On Error Goto 0 but that would be an easy way out and we won't learn anything by taking easy ways during training = )

thanks guys!
0
Comment
Question by:developingprogrammer
3 Comments
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 200 total points
ID: 39936564
You could test with Dir first:-
Dim file As Variant
   file = Dir(currentproject.path & "\Working Folder\*")

if file <>"" then
   Kill CurrentProject.Path & "\Working Folder\*"
else
...
0
 
LVL 24

Accepted Solution

by:
Bitsqueezer earned 300 total points
ID: 39947280
Hi,

I would use On Error Resume Next because this kind of error is of no interest for the user. The command works if no error occurs and that's enough. If you want to be super exact then you could go on with a "select case" statement going through all possible errors you wish to distinguish so you can determine which one to forward to the user and which to surpress. There are lots of possible errors coming from the file system so that's really a hard job. So if anything really depends on the result you could use On Error Resume Next and then use commands to test if the folder is really empty and display to the user that this didn't work maybe with an additional error message coming from the system - that's really enough, I think.
Think of possible reasons like deleting a folder on a write protected medium, on a medium like USB drive which the user removed right before, right problems with a network folder, shutdown of a server and, and, and... do you really want to test all possible file system reasons? I think that's not needed at all.

Cheers,

Christian
0
 

Author Comment

by:developingprogrammer
ID: 39949515
hi regmigrant, thanks for your help! i was thinking of using that line of code as i've used that before to test for files before writing to the network drive, but i guess i was looking for a one liner code (if it exists at all - which it doesn't now i've learnt ha = )   )

hi Christian!

yup i fully agree with you, i'm thinking that if there is a one liner code to test the folder then i'll use that, if not i'll use on error resume next because that is the most economical way of doing it and then i can use my time to focus on more productive development. thanks for confirming it and once again sharing more insight on the right mindset for programming, not just the technical code itself! = )
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

708 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

11 Experts available now in Live!

Get 1:1 Help Now