Solved

delete all files from folder even if folder is blank

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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