Solved

delete all files from folder even if folder is blank

Posted on 2014-03-18
3
510 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export  Access Query To Excell 16 46
Explanation of Access VBA code 2 36
Keeping a combo box up to date for other users 5 23
ms access 2013, running .mdb 2 30
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

22 Experts available now in Live!

Get 1:1 Help Now