+ Reply to Thread
Results 1 to 33 of 33

How delete last item for file name using inputbox

  1. #1
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    How delete last item for file name using inputbox

    Hi,
    I would delete last item is existed in file name specific in folders and sub folders in this device
    C:\Users\OMMR\Desktop\DEL


    here is some files names
    C:\Users\OMMR\Desktop\DEL\ abfgtt6666-00 accrrt.xlsm
    C:\Users\OMMR\Desktop\DEL\out\nhjyyuuu12000 redffg yu7888.xls
    C:\Users\OMMR\Desktop\DEL\out\files\nbhjh 20000asd hjyu777 ghjytt.pdf

    so what I want to show input box to write file name when click ok then delete last item , if click cancel don't delete.
    example : show inputbox and write abfgtt6666-00 accrrt when click ok will become abfgtt6666-00 like this within device C:\Users\OMMR\Desktop\DEL\abfgtt6666-00
    the last item will be part alone by contains space before it.
    thanks

  2. #2
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    Hi,
    just to be sure.
    Should it work like this?:
    1. I call the inputbox,
    2. I enter the text string ("abfgtt6666-00 accrrt") - without the extension,
    3. after confirming "yes", the program will search for a file that starts with the text from the input box.
    4. The program will change the file name so that it keeps the first part of the text:
    - abfgtt6666-00 accrrt.xlsm => abfgtt6666-00.xlsm
    - nhjyyuuu12000 redffg yu7888.xls => nhjyyuuu12000 redffg.xls
    - nbhjh 20000asd hjyu777 ghjytt.pdf => nbhjh 20000asd hjyu777.pdf

    Is that right?
    Question: - how do I know what the last part is? For example. "nhjyyuuu12000 redffg yu7888.xls" ?
    Question: - can there be more files with the same name. In a different directory, or with a different extension?

    m.s.

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,354

    Re: How delete last item for file name using inputbox

    Try it like this: I'm not sure what you want to do with the string once the last "word" is removed....


    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    Is that right?
    yes
    Question: - how do I know what the last part is? For example. "nhjyyuuu12000 redffg yu7888.xls" ?
    I have no idea how specify last item. I suppose function could do that like Len.
    Question: - can there be more files with the same name. In a different directory, or with a different extension?
    yes , sorry I don't mentioned that.

  5. #5
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    Try it like this: I'm not sure what you want to do with the string once the last "word" is removed....
    I will check it .

  6. #6
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    @Bernie Deitrick
    doesn't delete anything when I go to directory!

  7. #7
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    Hi Omar M,
    press "Start"
    m.s.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    press "Start"
    not really how works?!
    I wrote into inputbox asdff4 jk788 iiio yu777 and show me without change !
    doesn't delete anything !

  9. #9
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    corrected
    m.s.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    great!
    just I would fix problem for duplicate same files names . I would delete one by one by show inputbox every time when there are many duplicates same files name in many folders .
    if I click cancel then don't delete and show inputbox again to delete same file name fro another folder.
    look if I have file name like this arrrrb ghhhhh hhhhh
    and arrrrb ghhhhh hhhhh is existed in different location in folders , when show inputbox and write arrrrb ghhhhh hhhhh and click cancel
    then will skip arrrrb ghhhhh hhhhh is existed in first folder and keep inputbox contains arrrrb ghhhhh hhhhh and move to second folder and when click ok then delete it and move third folder to delete it when click ok .

  11. #11
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    Hi Omar M,
    deleting duplicate files is added. I just modified the method a bit:
    - you will see "Userform1",
    - in "Listbox1" there is a list of directories and files, in the order in which the program searches for them,
    - in "Listbox2" there is a list of renamed files
    --- if there are files with the same name but different extensions, they are separated by an empty line,
    --- files to be deleted have an "X" at the beginning of the line,
    --- by clicking on the given line, the "X" changes to "" (the file is not deleted),
    --- by clicking again, the "X" is reset,
    - marked files are deleted only after pressing the "Deleting marked files!" button

    I tested several options, but I might have overlooked something - so test.
    m.s.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    when select duplicates file from listbox1 will delete for all of duplicates !
    I suppose delete just selected item from listbox1 , right?
    Last edited by Omar M; 07-02-2025 at 02:01 PM.

  13. #13
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    files marked with an "X" will be physically removed from the disk.
    m.s.delete last item v4.jpg

  14. #14
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    should delete last item for duplicates files , not delete file when select one of them!
    should keep the duplicates files .

  15. #15
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    but in #10 you write about "removal".

  16. #16
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    sorry I don't clarify so !
    should delete item , not file.

  17. #17
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    we can continue by:
    - entering the file name "xxxx yyy" in the "Inputbox",
    - a list of files with the same name "xxxx yyy" will be displayed in "Listbox2",
    - clicking on a line in "Listbox2" will mark the files to be renamed ("X"),
    - clicking on the button will rename the marked files (the last part "yyy" will be removed from them)
    Do you agree?

  18. #18
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    - clicking on the button will rename the marked files (the last part "yyy" will be removed from them)
    this is not clear for me !
    should remove last part for the selected item in listbox2, not delete all of them at once , because I need keep the others
    do you understand me what I would or...?

  19. #19
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: How delete last item for file name using inputbox

    - you enter text in the "Inputbox",
    - a list of found files ("Listbox2") will appear,
    - click on the lines where you want to remove the last part of the file name (X),
    - click on the "Renaming marked files!" button,
    - the code will modify the name of only the marked files.
    m.s.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    awesome work!
    thank you so much.

  21. #21
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,730

    Re: How delete last item for file name using inputbox

    MSDols has you covered and all this is is another way.

    You asked for the last part of the file name to be removed but I have been waitng for the "What do I need to change if I want to delete another part"

    If you write some code and rely on typing data into boxes, there is always a possibility of mistakes. (spelling, capitalization, extra or missing spaces etc etc)
    That's one of the reasons we see so often "It does not work"
    This code will enter all the pertinent data through the code so only selecting is required which eliminates the possible spelling problems.

    Ensure that the folder name on the desktop is right in the code.
    Click on the "UserForm2" button
    On the UserForm that opens up, click on the "Click here to make your choice" button
    It will open the folder on the desktop.
    Select the file that you want to rename (delete part) and click on open at the bottom right.
    You will have the selected file name with path in ListBox1 and the parts of the split filename in ListBox2
    In ListBox2, select the part that you want to remove.
    The new file name, including path, will be in ListBox3
    Click on the "Save As New Name" button to rename the file.

    If you think that you can use it, do so. If not, bin it.

    Note: No Error checking included.

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    The inherent weakness of the liberal society: a too rosy view of humanity.

  22. #22
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    @jolivanes
    wow!
    this is really impressive!
    just show me error if when open dialog to select file. if I click cancel when I don't select file then will show me invalid procedure call or argument in this
    Please Login or Register  to view this content.
    by the way I notice when select the target item for deletion should show me new file how become but if I select new item by move to next item
    will show me new file name based on first time I selected prevoius item in listbox3, not based on new selected item in listbox2 but will rename correctly within folders!
    Last edited by Omar M; 07-09-2025 at 07:17 AM.

  23. #23
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,730

    Re: How delete last item for file name using inputbox

    It all works as advertised for me with folders and files as you have in Post #1.
    Mind you, it is a cobbled together exercise just to see if it is possible just in case you wanted another part deleted.
    Do all the files that you have in these folders have 2 or more parts to their file name?

  24. #24
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    Do all the files that you have in these folders have 2 or more parts to their file name?
    some files one part and the others two or three or four ....

  25. #25
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,569

    Re: How delete last item for file name using inputbox

    If I understand correctly this should address your concerns.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  26. #26
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,730

    Re: How delete last item for file name using inputbox

    Thanks Rudi at @bakerman2

    @Omar M
    Post #24. How do you envision to delete part of a filename if there is only one part?

  27. #27
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    @bakerman
    may you clarify what's difference?
    sometime show file not found Name Me.LB1.List(0) As Me.LB3.List(0) !

  28. #28
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    Post #24. How do you envision to delete part of a filename if there is only one part?
    should not delete it.

  29. #29
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,569

    Re: How delete last item for file name using inputbox

    I just used another method to retrieve the filename.
    Try switching this line
    Please Login or Register  to view this content.
    back to
    Please Login or Register  to view this content.
    and see if it still happens.

  30. #30
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    thanks bakerman .
    may you cancel delete item if the file name contains one part,please?
    like show message" you can't rename because file contains one part"

  31. #31
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,730

    Re: How delete last item for file name using inputbox

    Which code are you using?

    One way would be to add a line like so in the code from Post #21
    Please Login or Register  to view this content.

  32. #32
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,730

    Re: How delete last item for file name using inputbox

    BTW, if your file name starts with an empty space, (Char 32), the suggestion in Post #31 does not work.
    I don't think you can start a filename with a space but some people seem to be able to come up with the impossible!!!!!
    Last edited by jolivanes; 07-14-2025 at 09:14 PM.

  33. #33
    Forum Contributor
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    100

    Re: How delete last item for file name using inputbox

    BTW, if your file name starts with an empty space, (Char 32), the suggestion in Post #31 does not work.
    OK understood.
    thank you so much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Error message when inputbox entry does not match UF listbox item
    By Retroshift in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-11-2022, 12:34 PM
  2. Replies: 0
    Last Post: 01-29-2022, 05:37 AM
  3. [SOLVED] Adding delete code on userform to delete a row on sheet by selecting item on a listbox
    By darbar76528 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-07-2020, 12:50 PM
  4. Find available item from an Inventory file, searcg alternative Item if not avail
    By Oscar_Italy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2017, 08:34 AM
  5. [SOLVED] Opening file with a macro - Pick file [week] with inputbox
    By VitoBdG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2015, 08:41 AM
  6. [SOLVED] Open excel file by providing a part of file name through inputbox
    By sbabu16 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-12-2014, 05:30 AM
  7. Delete Rows Using Inputbox
    By japorms in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-08-2008, 05:16 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1