removing the path in a listbox.

My app searches the drive for specific files then displays them in a listbox.(The whole path). The user then selects from there. What I would like to do is display only the filename but have the program remember where the file is located for them to use. I suspect that I could have 2 listboxes displaying the same info, but one not visible that includes the path.

My question is how can I display only the filenames and not the path too?
kkoserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
Here's a function that takes out the filename out of a 'fullpath filename' such as 'D:\Here's the filename\Filename.ext'..

Function sGetFileName(sFullPath As String) As String

  Dim i As Integer

  For i = Len(sFullPath) To 1 Step -1
    If (Mid$(sFullPath, i, 1) = "\") Then Exit For
  Next

  sGetFileName = Mid$(sFullPath, i + 1, Len(sFullPath) - i)

End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VbmasterCommented:
Hmm change the line..

  sGetFileName = Mid$(sFullPath, i + 1, Len(sFullPath) - i)

...to..

  sGetFileName = Mid$(sFullPath, i + 1)

No need for that last parameter.
0
kkoserAuthor Commented:
how do I make the listbox display only the filename using your function.



I use this line to add filenames to the listbox now.



List1.AddItem strFind(lngCount)



do I use your function before or after this?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

VbmasterCommented:
As I read it you were gonna have two textboxes, one visible with only filenames and one invisible with full pathname, right?

If this is so you can just add

List2.Additem sGetFileName(strFind(lngCount))

after or before that line of yours. Then you just hide the List1 by setting it's Visible property to False.
0
kkoserAuthor Commented:
It works like a charm. Now I can shrink the Listbox for a little more room.

Can I ask how to synchronize the 2 listboxes properly here, or should i ask a different question?

Ican bump up the points some...
0
mark2150Commented:
Watch out for the .Sorted property. If your listboxes are sorted then this won't work.

You can also get good results with a sorted list box if you just use a string array. Keep expanding the array with REDIM PRESERVE when you add an item to the list. Then you set the List1.ListData property to the current index of the array. When you read back out, look at the .ItemData property of the .Selected item and then use that as your array index. This will give you the proper file.

This sounds complex but really isn't. It allows you to have your list sorted (helpful with just filenames) and still keep the full path where you need it.

DIM Array() AS String
DIM N AS Integer
N = 0

Saving becomes:

List1.Additem FileName
N = N + 1
REDIM Preserve Array(N)
Array(N) = FilePath
List1.ItemData(List1.NewIndex) = N

Lookup becomes something like:

FilePath = Array(List1.ItemData(List1.ListIndex))

Use something like VBmasters code to separate out the filename from the path.
            
M
0
kkoserAuthor Commented:
I forgot. Mark, Did you give me the Drive Search code that i am using now?
0
VbmasterCommented:
What do you mean with synchronizing the two listboxes? I thought they were rather synchronized ;)

0
VbmasterCommented:
What do you mean with synchronizing the two listboxes? I thought they were rather synchronized ;)

0
kkoserAuthor Commented:
Must be an echo in here.:)

An example would be that if i had selected an item from one listbox, it would update the other listbox to the same selection.
0
VbmasterCommented:
No, there's no echo it's just me being a little schizophrenic.

But I don't understand WHY you would want to do that, since that other list is invisible.. No need to make it slower than it already is..

(The code would be, if you by some good reason (??) would wanna do this, List2.Listindex = List1.Listindex in the List1_Click event )

0
kkoserAuthor Commented:
Not slow at all. Working awesome. All I am doing is letting the user select a .avi and decide whether or not they want to play it.

Small program, that's all. But it helps me learn. Thanx.

0
kkoserAuthor Commented:
How do i do the same thing but with one filename in a textbox?
0
kkoserAuthor Commented:
you're right mark2150. When i changed the .sorted property to True, it didn't stay in synch. I don't understand how to apply your code. When i tried to paste,

DIM Array() AS String

It said it was looking for an expected identifier.

Where does this go?

 DIM Array() AS String
 DIM N AS Integer
 N = 0



 Saving becomes:
 
Where do i put this?

  List1.Additem FileName
  N = N + 1
  REDIM Preserve Array(N)
  Array(N) = FilePath
  List1.ItemData(List1.NewIndex) = N

  Lookup becomes something like:

  FilePath = Array(List1.ItemData(List1.ListIndex))
0
mark2150Commented:
It probably doesn't like the word ARRAY as a variable. It's a *placeholder* for the array name that you would use in your code. As are FilePath and FileName. These tend to be reserved words, but the intent is to show *what* goes where instead of actual names. Kind of {Insert your array name here} but that's too much to type.

In effect your using a 2nd (invisible) list box as the storage array for the full paths. That's fine, if a little inefficient since you don't need all the other bells and whistles that a list box comes with as you're just treating it as an array.

In my example we go ahead and create an array (perhaps MyArray would have been a better choice in the example...) to hold the info.

The reason that the .Sorted property screws everything up in the first example is that the paths & files order differently that just the files alone. If you have say two files:

C:\DirA\Z.AVI
C:\DirB\A.AVI

Then the "path" list will sort in the order shown but the "file" list will reverse 'em. Oops!

Anyway, replace the Array() with, say, MyArray() and likewise rename the FilePath and FileName vars and it should work.

Sorry for such sloppy work on my part but it was late and I was more concerned about illustrating the concept than the actual code.

M
0
kkoserAuthor Commented:
At Load it looks like this:

Where do I put your code?
Do I have to have the listboxes' .sorted property to False?



Private Sub Form_Load()

   
Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long
Private Sub Form_Load()

Set objFind = New clsFileFinder

Me.MousePointer = vbHourglass
DoEvents
Command1.Enabled = False
lngFound = objFind.LocateFileLocally("*.mp3", strFind)
If objFind.blnFindAbort Then
    Exit Sub
End If
Command1.Enabled = True
Me.MousePointer = vbDefault
For lngCount = 0 To lngFound - 1
    List1.AddItem sGetFileName(strFind(lngCount))
    List2.AddItem strFind(lngCount)
Next

Set objFind = Nothing

End Sub





0
kkoserAuthor Commented:
the *.mp3 files have been changed to *.avi.  Sorry about that.
0
kkoserAuthor Commented:
o.k..... I think i got it to work. a little. It searches the hard drive for files then lists only 1 file in the listbox. 27 times. Which is how many .avi files are on the drive.

What am i doing wrong?
0
kkoserAuthor Commented:
Private Sub Form_Load()

Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long
Dim MyArray() As String
Dim N As Integer
N = 0

'Lookup becomes something like:
'FilePath = MyArray(List1.ItemData(List1.ListIndex))

Set objFind = New clsFileFinder

Me.MousePointer = vbHourglass
DoEvents
Command1.Enabled = False
lngFound = objFind.LocateFileLocally("*.avi", strFind)
If objFind.blnFindAbort Then
    Exit Sub
End If
Command1.Enabled = True
Me.MousePointer = vbDefault
For lngCount = 0 To lngFound - 1
'Saving becomes:
List1.AddItem sGetFileName(strFind(longCount))
N = N + 1
ReDim Preserve MyArray(N)
MyArray(N) = FilePath
List1.ItemData(List1.NewIndex) = N

List2.AddItem (strFind(longCount))
N = N + 1
ReDim Preserve MyArray(N)
MyArray(N) = FilePath
List2.ItemData(List2.NewIndex) = N

''    List1.AddItem sGetFileName(strFind(lngCount))
''    List2.AddItem strFind(lngCount)
Next

Set objFind = Nothing

End Sub
0
kkoserAuthor Commented:
This line gives me an ivalid use of an array error message.



FilePath = MyArray(List1.ItemData(List1.ListIndex))
0
mark2150Commented:
Check to see that List1.ListIndex is =>0 or else no item has been selected. If this is the case it'll return -1 and that makes the array refernce go TILT!

M
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.