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
Solved

Removing a file with a specific file type

Posted on 2007-12-04
16
317 Views
Last Modified: 2008-02-01
How can I go about deleting a file in a folder based on the file type.  The file name is not known but the extension is. So basically I need file     *.bkup to be deleted.
0
Comment
Question by:fuzzymallets1
  • 4
  • 4
  • 4
  • +2
16 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20404097
*.bkup could point to one file, or it could point to 1000.  Are you sure that's what you want to do?
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20404100
Sample code that in vbscript to delete txt file. You can also use filetype. I have used tempfolder. if it is for otherfolder instead of Set ofolder = ofso.GetSpecialFolder(2) use setofolder = ofso.getfolder("yourfolderpath")


Option Explicit
on error resume next
      Dim oFSO
      Dim sDirectoryPath
      Dim oFolder
      Dim oFileCollection
      Dim oFile
      Dim iDaysOld

'Customize values here to fit your needs
'      iDaysOld = 1
      Set oFSO = CreateObject("Scripting.FileSystemObject")
Set ofolder = ofso.GetSpecialFolder(2)
      set oFileCollection = oFolder.Files

      For each oFile in oFileCollection
'            msgbox ofile.type
msgbox ofile.name
            if ucase(right(ofile.name))="Txt" then oFile.Delete(True)
      
      Next
      Set oFSO = Nothing
      Set oFolder = Nothing
      Set oFileCollection = Nothing
      Set oFile = Nothing

msgbox "Temp folder cleaned",vbokonly,"Shanmuga Sundaram"
0
 
LVL 5

Author Comment

by:fuzzymallets1
ID: 20404216
Yes I need it to remove all *.bkup.  
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 38

Expert Comment

by:PaulHews
ID: 20404258
In .NET:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Path As String = "C:\Temp"
    Dim Files() As String = Directory.GetFiles(Path, "*.bkup")
    For Each FileName As String In Files
        File.Delete(FileName)
    Next

End Sub
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20404278
shasunder, please note the topic area.  That code is VBScript.
0
 
LVL 10

Expert Comment

by:vadim63
ID: 20404282
Kill("C:\TEMP\*.bkup")
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20404330
I'm torn between recommending PaulHews solution and vadim's.   On the one hand, I don't normally recommend using methods like Kill() or Shell(), in favor of the newer counterparts.  On the other hand, the more 'sanctioned' method posted by Paul has the potential to leave the job 1/2 done in the case where an exception occurs or the process is killed abnormally.
0
 
LVL 5

Author Comment

by:fuzzymallets1
ID: 20404331
The place that the bkup files are located are in the folder that the program is running from. so would I need to leave out the path info?
0
 
LVL 10

Expert Comment

by:vadim63
ID: 20404385
Kill(Application.StartupPath & "*.bkup")
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 20404406
>so would I need to leave out the path info?

Better to determine the path at runtime, or you may run into problems, especially while debugging.

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Path As String = System.AppDomain.CurrentDomain.BaseDirectory()
        Dim Files() As String = Directory.GetFiles(Path, "*.bkup")
        For Each FileName As String In Files
            File.Delete(FileName)
        Next

    End Sub
End Class
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20404436
>so would I need to leave out the path info?

Heavens no!  It might appear to work at first, but what happens if it doesn't find any files in the .exe's folder?  It will check the environment path, and you could end up deleting files in other folders!
0
 
LVL 10

Assisted Solution

by:vadim63
vadim63 earned 250 total points
ID: 20404437
Supposed to be
"\*.bkup"
Kill(Application.StartupPath & "\*.bkup")
0
 
LVL 5

Author Comment

by:fuzzymallets1
ID: 20404469
I have tested both of your suggestion and they both work great.
0
 
LVL 10

Expert Comment

by:vadim63
ID: 20404472
OK, good luck!
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20404524
>It will check the environment path, and you could end up deleting files in other folders!

I believe it will use the "startup path" of the shortcut used to launch it.  In the IDE that will not be the same as the bin folder, so you will get unexpected results.  And I'm pretty sure it will not search the environment path.

>On the other hand, the more 'sanctioned' method posted by Paul has the potential to leave the job 1/2 done in the case where an exception occurs or the process is killed abnormally.<

Kill can also have an exception, and is less granular than the File methods, so gives you less control over what happens when you run into an exception.

Kill will raise an exception when files are not found, so you have to trap that as well.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20404901
You're right- I had read an article recently that was in the back of mind that led me to believe it would check the path if nothing was found.  I went back and tracked it down.  It was posted on a Win32 API blog, but it was talking about how windows works, so it still applies.  Here's the link:
http://blogs.msdn.com/oldnewthing/archive/2007/10/23/5612082.aspx

However, now that I've re-read it I see it refers to other providers doing things "behind Windows back."
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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