dcsimg
Results 1 to 16 of 16
  1. #1

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

    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
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    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
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    872

    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
    132

    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
    132

    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
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    872

    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
    23,498

    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
    4,194

    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

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

    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
    4,194

    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  11. #11
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    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
    132

    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
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    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
    132

    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
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    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
    132

    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
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.