Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

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,
0
edfreels
Asked:
edfreels
  • 4
  • 2
1 Solution
 
Saurabh Singh TeotiaCommented:
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

0
 
leonstrykerCommented:
You would need to use a KILL command:

Kill strFilePathName

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

Leon
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
edfreelsAuthor Commented:
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.
0
 
leonstrykerCommented:
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
0
 
leonstrykerCommented:
Here is how to run a Shell command from C# http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx

Here is how to delete a file from C# http://dotnetdud.blogspot.com/2008/11/delete-files-using-c-delete-files-using.html


private static bool DeleteFile(string sFileName)

{

try

{

if (File.Exists(sFileName) == true)

{

File.Delete(sFileName);

return true;

}

else

{

Console.WriteLine("File doesnot exist");

return false; }

}

catch (UnauthorizedAccessException ex)

{

// File in Use

Console.WriteLine("You do not have required permission for this operation " + ex.Message);

return false;

}

catch (IOException exIO)

{

// File in Use

Console.WriteLine("File in use " + exIO.Message);

return false;

}

catch (Exception ex)

{//Common Exception

Console.WriteLine(ex.Message);

return false;

}

}

Open in new window

0
 
edfreelsAuthor Commented:
Sorry about the delay, I have been bogged down with something else; Thanks leonstryker for the solution, it works wonderfully.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now