Results 1 to 15 of 15

Thread: [RESOLVED] Remove the last 4 letters from my list box list names.

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Resolved [RESOLVED] Remove the last 4 letters from my list box list names.

    Hello,

    I have a list box that loads .bmp files and I can view them by clicking on the selected item.

    My question is how do I remove the .bmp from the end of the name?

    My list box looks like this:

    Pic1.bmp
    pic2.bmp

    And I want it to look like this:

    Pic1
    Pic2

    Thank you and have a great day!

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Remove the last 4 letters from my list box list names.

    Can you give the code you use to load the filenames in?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Remove the last 4 letters from my list box list names.

    Hello Chem,

    Thank you for your reply.

    Here is the code I am using.

    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.     frmMain.List1.AddItem xFile.Name
    9. Next
    10.  
    11.     Dim i As Long
    12. For i = frmMain.List1.ListCount - 1 To 0 Step -1
    13.     If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    14.     Else
    15.        frmMain.List1.RemoveItem i
    16.     End If
    17. Next

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Remove the last 4 letters from my list box list names.

    Use this:

    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.     frmMain.List1.AddItem [b]Left(xFile.Name, Len(xFile.Name) - 4)[/b]
    9. Next
    10.  
    11.     Dim i As Long
    12. For i = frmMain.List1.ListCount - 1 To 0 Step -1
    13.     If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    14.     Else
    15.        frmMain.List1.RemoveItem i
    16.     End If
    17. Next

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Remove the last 4 letters from my list box list names.

    Oh whoops. I figured it was more of a parsing job. Didn't read the question

    Jacobs code (converted to your use) will work.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Remove the last 4 letters from my list box list names.

    stilekid007

    Jacob Roman's Code will work perfectly for you current program, but if in the future you will be adding files to your list that have a longer extention then you may want to do somthing like this...


    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5. [B]Dim FileName() As String[/B]
    6.  
    7. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    8. For Each xFile In xFolder.Files
    9.            
    10.     [B]FileName = Split(xFile.Name, ".")
    11.     frmMain.List1.AddItem FileName(0)[/B]  
    12.  
    13. Next
    14.  
    15.     Dim i As Long
    16. For i = frmMain.List1.ListCount - 1 To 0 Step -1
    17.     If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    18.     Else
    19.        frmMain.List1.RemoveItem i
    20.     End If
    21. Next

    Cheers All

    Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  7. #7

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Remove the last 4 letters from my list box list names.

    Hello,

    Thank you for the code Jacob.

    I see that your code works great. Now that I try it I have testers that test the file to make sure it is a image file. I have to different testers in my program and without the .bmp on the end it doesn't detect it as a image file.

    So I guess I will just have to deal with that.

    Thank you so much for the code though! It works great!
    Stilekid007
    P.S. Thank you Hojo as well! I will keep it in mind for the future!
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  8. #8
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    stilekid007

    You may need to check if its a ".bmp" file before you remove the extention and add it too your list

    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.    
    9.       If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    10.               frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
    11.       Else
    12.                'don't add to list
    13.       End If
    14.  
    15. Next

    Hope this helps

    Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  9. #9

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    Quote Originally Posted by Hojo
    stilekid007

    You may need to check if its a ".bmp" file before you remove the extention and add it too your list

    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.    
    9.       If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    10.               frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
    11.       Else
    12.                'don't add to list
    13.       End If
    14.  
    15. Next


    Hojo
    Well the problem with this code is this line:
    Code:
           If Right(frmMain.List1.List(i), 4) = ".bmp" Then
    It will never be true because the list box has nothing in it at this time. The code would have to be modified to have it check the file name without the listbox. So we would have to check it against this "(xFile.Name, Len(xFile.Name)".

    I am not smart enough to make a code that acttually works with that. I run into all kinds of errors. I can't right my own code to well
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  10. #10

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    HAHA! WAIT! I GOT IT TO WORK!

    VB Code:
    1. Dim strImagePath As String
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.  
    9. strImagePath = ("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\" & (xFile.Name))
    10.  
    11.      If Dir(strImagePath) <> "" And (Right(strImagePath, 4) = ".bmp" Or Right(strImagePath, 4) = ".jpg" Or Right(strImagePath, 5) = ".jpeg") Then
    12.               frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
    13.              
    14.               Else
    15.               'do nothing
    16.  
    17.     End If
    18. Next

    Check it out!

    I actually made my own code! (WORK) That is simply 100% AMAZING!

    Thank you for the inspiration!
    You guys are really awsome!
    This works great now!
    :P
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  11. #11
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    Sorry My bad

    The problem with just cutting and pasting code.

    Try This

    VB Code:
    1. frmMain.List1.Clear
    2. Dim fso As New Scripting.FileSystemObject
    3. Dim xFolder As Folder
    4. Dim xFile As File
    5.  
    6. Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
    7. For Each xFile In xFolder.Files
    8.    
    9.       If Right([B]xFile.name[/B], 4) = ".bmp" Then
    10.               frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
    11.       Else
    12.                'don't add to list
    13.       End If
    14.  
    15. Next

    Sorry again

    hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  12. #12
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    Well Done Stilekid007

    Its always a great feeling when things start to work.

    enjoy the rest of your day.

    Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  13. #13

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    LOL Thank you Hojo! I guess your code is more simple then mine (above your post) But it is still amazing that mine works haha! Ok, thank you for your help!

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  14. #14
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    Sorry, a little late with the post but I thought I'd mention it to you anyway --

    You could have just easily used the GetBaseName method of the FileSystemObject...

    VB Code:
    1. Dim fso As FileSystemObject
    2. Dim sBaseName As String
    3.  
    4. Set fso = New FileSystemObject
    5.  
    6. ' this will return the name of the file without the extension
    7. sBaseName = fso.GetBaseName(sPathToFile)

  15. #15

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: [RESOLVED] Remove the last 4 letters from my list box list names.

    Hey Zebula! Thank you for the code! That would have been a bit less code for getting the path name.

    Thank you again and have a great day!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

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