Results 1 to 4 of 4

Thread: [RESOLVED] Delete files with the same ending

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2022
    Posts
    22

    Resolved [RESOLVED] Delete files with the same ending

    Hello, I have the following images in a folder:
    - 202022242_univ_cnt_1.jpg
    - 202022242_univ_sqr.jpg

    There are many images, the numerical value changes, but the _univ_sqr.jpg data does not change, how can I delete only the images that end in _univ_sqr.jpg?

  2. #2
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Delete files with the same ending

    match with the last 9 characters of the name or 13 of whole name with ext

    like
    Code:
    if right(filename,9)="_univ_sqr.jpg" then deletefile(filename)
    assuming that you have a deletefile() function

    its the right(name,length) bit that you could use

    just thinking out loud

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,076

    Re: Delete files with the same ending

    For the love of God, please do not use that legacy code.

    Do this instead, get the filenames by calling Directory.GetFiles, pass the second argument which is a search pattern, loop over the results, call File.Delete.

    Here is an example:
    Code:
    Dim filesToDelete = IO.Directory.GetFiles("my-directory", "*_univ_sqr.jpg")
    For Each fileToDelete In filesToDelete
        IO.File.Delete(fileToDelete)
    Next
    You should sprinkle in some additional null checks in there, but that is the basic gist.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2022
    Posts
    22

    Re: Delete files with the same ending

    Thank you very much friends, it has worked perfectly for me, I replaced Delete with Move, it was easier for me to move the images that I only needed and then delete the directory.

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