Results 1 to 16 of 16

Thread: [RESOLVED] Kill with wild card

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Resolved [RESOLVED] Kill with wild card

    Hi,
    In some circumstances I create a temporary file whose name contains the date.
    I did not think that in the long run I would have a lot of useless files.
    I would like to delete them, the problem is that I only know a part of the file name.
    I tried something like Kill "Filename ??????" but I get "File not found".
    An idea to solve this ?
    thanks

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Kill with wild card

    Hi,

    here a sample to Kill all files in a Folder older than 3 Days

    Code:
    Option Explicit
    Dim sPath As String
    Dim sFile As String
    Dim xDate As Date
    Dim Tage As Integer
    
    Private Sub Command1_Click()
    Tage = 3
    'delete files older than 3 Days
    xDate = DateAdd("D", -Tage, Format(Now, "dd.mm.yyyy"))
     
    sPath = "c:\TestText\"
     
    ' Ordner durchsuchen
    sFile = Dir$(sPath & "*.*", vbNormal)
    Do Until sFile = ""
      If sFile <> "." And sFile <> ".." Then
        ' älter als xDate?
        If FileDateTime(sPath & sFile) < xDate Then Kill sPath & sFile
      End If
      sFile = Dir$
    Loop
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Kill with wild card

    Modifying the code that Chris posted:

    Code:
    Option Explicit
    
    Private Sub DeleteFiles(sFolder As String, sFilenamePart as String)
    Dim sFile As String
     
    sFile = Dir$(sFolder & "*.*", vbNormal)
    Do Until sFile = ""
      If Instr(sFile, sFilenamePart) > 0 Then Kill sFolder & sFile
      sFile = Dir$
    Loop
    End Sub
    PS: It is air code.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Kill with wild card

    Thank you for your reply.
    However in MSDN https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx I read
    Kill supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.
    Why do I get "File not found" ?

    Here some files I would like to delete
    Angles - Hervé Reef Tools 160115 © www.rudyv.be.par
    Angles - Hervé Reef Tools 160414 © www.rudyv.be.par
    Angles - Hervé Reef Tools 171207 © www.rudyv.be.par
    Angles - Hervé Reef Tools 180122 © www.rudyv.be.par
    ...
    Kill "Angles - Hervé Reef Tools ?????? © www.rudyv.be.par" ==> 53 file not found
    The path is correct.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Kill with wild card

    Quote Originally Posted by Eduardo- View Post
    Code:
    sFile = Dir$(sFolder & "*.*", vbNormal)
    Why not
    Code:
    sFile = Dir$(sFolder & sFilenamePart" & *.par", vbNormal)
    so If Instr is useless, you can kill all Dir$ files ?

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Kill with wild card

    Quote Originally Posted by Herve_be View Post
    Why not
    Code:
    sFile = Dir$(sFolder & sFilenamePart" & *.par", vbNormal)
    If the file name starts with that, yes.
    Note: I don't remember if Dir$ function allows wilcards in the middle.
    But you could test such things for yourself, couldn't you?

    Quote Originally Posted by Herve_be View Post
    so If Instr is useless, you can kill all Dir$ files ?
    What do you mean "useless", please be more clear.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Kill with wild card

    However in MSDN
    that is .net

    Why not sFile = Dir$(sFolder & sFilenamePart" & *.par", vbNormal)
    with a small adjustment should work fine
    Code:
    sFile = Dir$(sFolder & sFilenamePart & "*.par", vbNormal)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: Kill with wild card

    If it were me doing this, I'd build a loop using the Dir$(sFolder & sFileWithWildcard) on each iteration of the loop. In other words, I'd never use the Dir$[no arguments] option. That way, your deletions wouldn't rein havoc on the function of the Dir$[no arguments].

    Just my humble opinion.

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Kill with wild card

    Quote Originally Posted by Elroy View Post
    I'd never use the Dir$[no arguments] option. That way, your deletions wouldn't rein havoc on the function of the Dir$[no arguments].
    I don't understand "rein havoc".

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: Kill with wild card

    "rein havoc" = "cause trouble"

    It just seems dubious to me to use Dir$[no arguments] while you're simultaneously deleting the files that it's returning. When you do things like this, it's always advisable to spin through the list backwards, so you don't have fetch-and-delete collisions. However, the Dir$ function gives you no way to tell it to go backwards.

    If I were insistent on using Dir$(path and name) and also Dir$[no arguments], I'd use them to build a list of files I wanted to delete (say, in a string array), and then be done with all the Dir$ stuff, and then delete my files.

    Just the way I'd do it.

    Good Luck,
    Elroy

    EDIT1: Also, after googling a bit, I guess I misspelled it. It should be "reign havoc".
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Kill with wild card

    Hi Herve,

    did you get this working now ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Kill with wild card

    Finally it works fine like this
    Code:
    Public Sub KillWaste(FileIdent As String) ' Parameter is the common part of the file name
    ParFileName = App.Path & "\" & FileIdent & "*.par"
    Dim Waste As String
    Waste = Dir$(ParFileName, vbHidden) ' Useless files are hidden
    Do Until Waste = ""
        SetAttr App.Path & "\" & Waste, vbNormal ' Kill does not delete hidden files
        Kill App.Path & "\" & Waste
        Waste = Dir$(ParFileName, vbHidden)
    Loop
    End Sub
    Thanks a lot for your help, problem solved.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: [RESOLVED] Kill with wild card

    does it also work if you want to search for the numbers in the Files like 160*.....

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: [RESOLVED] Kill with wild card

    Quote Originally Posted by ChrisE View Post
    does it also work if you want to search for the numbers in the Files like 160*.....
    Why should I do that ?

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: [RESOLVED] Kill with wild card

    Quote Originally Posted by Herve_be View Post
    Why should I do that ?
    I thought that was what you were looking to do... find and delete numbers starting with 160*

    Here some files I would like to delete
    Angles - Hervé Reef Tools 160115 © www.rudyv.be.par
    Angles - Hervé Reef Tools 160414 © www.rudyv.be.par
    Angles - Hervé Reef Tools 171207 © www.rudyv.be.par
    Angles - Hervé Reef Tools 180122 © www.rudyv.be.par
    ...
    Kill "Angles - Hervé Reef Tools ?????? © www.rudyv.be.par" ==> 53 file not found
    The path is correct.
    well you marked this as resolved, so I got it wrong

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: [RESOLVED] Kill with wild card

    Quote Originally Posted by ChrisE View Post
    I thought that was what you were looking to do... find and delete numbers starting with 160*
    Not exactly
    Quote Originally Posted by Herve_be View Post
    In some circumstances I create a temporary file whose name contains the date.
    In other words I wanted to delete files whose names are the same except 6 digits representing the date, so deleting
    Angles - Hervé Reef Tools*.par
    is all right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width