Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete all files from folder even if folder is blank

Posted on 2014-03-18
3
Medium Priority
?
545 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 800 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 1200 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

719 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