Link to home
Start Free TrialLog in
Avatar of edfreels
edfreelsFlag for United States of America

asked on

How to delete certain Excel Workbooks in a folder

Hello Experts!
I am wanting to check a folder that contains Excel Workbooks that are no longer needed, and Delete them.  I have scoured the boards here and have not found what I am looking for. Is there a way to search a filepath and delete the unwanted books? I appreciate your help on this. Thanks,
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

You can use the following code.
To use this code, You need to have Microsoft scripting runtime selected in library under-->Tools-->references.
Saurabh...

Private Sub filesdelets()
    Dim objFileSys As New FileSystemObject
    Dim objFolder As Folder

    Dim objFile As File

    Set objFolder = objFileSys.GetFolder("Your Path here")

    For Each objFile In objFolder.Files

        If InStr(1, objFile.Name, "abc.xls") > 0 Then objFile.Delete

   Next
End Sub

Open in new window

You would need to use a KILL command:

Kill strFilePathName

Leon
Kill works with out the need to reference the File System Object

Leon
Avatar of edfreels

ASKER

Okay saurabh726, I am looking for a C# solution, and leonstryker. The KILL command is not recognized with my C#, and, doesn't that simply close or terminate any open specified apps? Or, does is perform a delete as well? Just curious. The error I get with the KILL,...."The name "KILL" does not exist in the current context.
Well, you posted this in the Excel area as well as C# and both of us are looking at this question from that side, which to us means VBA. In any case, Kill is a Shell command so from C# you would need to run it as such. Not being too familiar with C#, I am not too sure how it is done.

Leon
ASKER CERTIFIED SOLUTION
Avatar of leonstryker
leonstryker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry about the delay, I have been bogged down with something else; Thanks leonstryker for the solution, it works wonderfully.