Solved

delete all files from folder even if folder is blank

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

738 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