Deleting a file from a folder using VBA

This question is probably pretty straight forward but given the consequences if I get it wrong I thought I would ask.

I have a table that has the name of all the files in a particular folder on my c: drive.  If I delete it out of the table I want it to delete the file out of the folder too.  I have the functionality to import the file names and to delete the file name out of the table I need to delete the file now out of the folder.  

How would I do this.  I get once the file is deleted it is gone, so that is why I am asking.
mtrussellAsked:
Who is Participating?
 
danishaniCommented:
You can use the Kill() statement in VBA to delete a FileName:

Kill Filename

HTH,
Daniel
0
 
Jeffrey CoachmanMIS LiasonCommented:
Just a note, that in some situations, "Kill" is prohibited in VBA because of certain security settings...

In this case you will have load a reference to the Windows Scripting Runtime Library in your VBA editor and use code like this:

Dim fso As FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile ("C:\YourFolder\YourFile.xls")



JeffCoachman
0
 
Patrick MatthewsCommented:
No points for this, please :)

You can run Jeff's code above without setting a reference by changing:

Dim fso As FileSystemObject

to

Dim fso As Object

Patrick
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
GRayLCommented:
Hi Patrick:  I have to ask: "Why would you want to?"  It has aspects of using a table saw without a guard ;-)
0
 
Patrick MatthewsCommented:
Ray,

1) Laziness :)

2) Not really applicable for the Scripting Runtime library, but I try to do most of my external references using late binding so as to keep maximum flexibility if there may be a range of versions in use

Patrick
0
 
mtrussellAuthor Commented:
thanks
0
 
GRayLCommented:
<...range of versions in use> -  Thanks, completely overlooked that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.