|
-
Jul 27th, 2005, 07:42 PM
#1
Thread Starter
Hyperactive Member
[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
 Originally Posted by stilekid007
-
Jul 27th, 2005, 08:00 PM
#2
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
-
Jul 27th, 2005, 08:02 PM
#3
Thread Starter
Hyperactive Member
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:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
frmMain.List1.AddItem xFile.Name
Next
Dim i As Long
For i = frmMain.List1.ListCount - 1 To 0 Step -1
If Right(frmMain.List1.List(i), 4) = ".bmp" Then
Else
frmMain.List1.RemoveItem i
End If
Next
Stilekid007
 Originally Posted by stilekid007
-
Jul 27th, 2005, 08:03 PM
#4
Re: Remove the last 4 letters from my list box list names.
Use this:
VB Code:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
frmMain.List1.AddItem [b]Left(xFile.Name, Len(xFile.Name) - 4)[/b]
Next
Dim i As Long
For i = frmMain.List1.ListCount - 1 To 0 Step -1
If Right(frmMain.List1.List(i), 4) = ".bmp" Then
Else
frmMain.List1.RemoveItem i
End If
Next
-
Jul 27th, 2005, 08:05 PM
#5
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
-
Jul 27th, 2005, 08:23 PM
#6
Lively Member
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:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
[B]Dim FileName() As String[/B]
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
[B]FileName = Split(xFile.Name, ".")
frmMain.List1.AddItem FileName(0)[/B]
Next
Dim i As Long
For i = frmMain.List1.ListCount - 1 To 0 Step -1
If Right(frmMain.List1.List(i), 4) = ".bmp" Then
Else
frmMain.List1.RemoveItem i
End If
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!
-
Jul 27th, 2005, 08:25 PM
#7
Thread Starter
Hyperactive Member
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!
 Originally Posted by stilekid007
-
Jul 27th, 2005, 08:33 PM
#8
Lively Member
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:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
If Right(frmMain.List1.List(i), 4) = ".bmp" Then
frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
Else
'don't add to list
End If
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!
-
Jul 27th, 2005, 09:09 PM
#9
Thread Starter
Hyperactive Member
Re: [RESOLVED] Remove the last 4 letters from my list box list names.
 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:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
If Right(frmMain.List1.List(i), 4) = ".bmp" Then
frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
Else
'don't add to list
End If
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
 Originally Posted by stilekid007
-
Jul 27th, 2005, 09:11 PM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] Remove the last 4 letters from my list box list names.
HAHA! WAIT! I GOT IT TO WORK!
VB Code:
Dim strImagePath As String
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
strImagePath = ("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\" & (xFile.Name))
If Dir(strImagePath) <> "" And (Right(strImagePath, 4) = ".bmp" Or Right(strImagePath, 4) = ".jpg" Or Right(strImagePath, 5) = ".jpeg") Then
frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
Else
'do nothing
End If
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
 Originally Posted by stilekid007
-
Jul 27th, 2005, 09:12 PM
#11
Lively Member
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:
frmMain.List1.Clear
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Documents and Settings\All Users\Application Data\Clipboard Data\Pictures\")
For Each xFile In xFolder.Files
If Right([B]xFile.name[/B], 4) = ".bmp" Then
frmMain.List1.AddItem Left(xFile.Name, Len(xFile.Name) - 4)
Else
'don't add to list
End If
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!
-
Jul 27th, 2005, 09:13 PM
#12
Lively Member
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!
-
Jul 27th, 2005, 09:14 PM
#13
Thread Starter
Hyperactive Member
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
 Originally Posted by stilekid007
-
Jul 27th, 2005, 10:12 PM
#14
Fanatic Member
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:
Dim fso As FileSystemObject
Dim sBaseName As String
Set fso = New FileSystemObject
' this will return the name of the file without the extension
sBaseName = fso.GetBaseName(sPathToFile)
-
Jul 28th, 2005, 08:19 AM
#15
Thread Starter
Hyperactive Member
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
 Originally Posted by stilekid007
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|