Solved

Just the dir name not the whole path

Posted on 2003-11-21
4
262 Views
Last Modified: 2010-05-03
Hello I'm using the DirListBox in my latest program but I have a little problem.
The data stored in the dir1 contains the whole path to a directory.
I only need the chosen directory.
Example:

dir1 = "C:\Documents and settings\all users\Desktop\"

I want just "Desktop" to be stored in a variable somehow.
I have a few ideas how I could do this and I'm sure they would work but it would be
MUCH work. So maybe someone here knows a fast efficient way to do this.
0
Comment
Question by:Shibjuh
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
dbrckovi earned 75 total points
ID: 9795522
Try this:
This will split your path to components (directories) separated by "\"

----------------------------------
Private Sub Dir1_Change()
   Dim vArray
   Dim i As Integer

   vArray = Split(Dir1.Path, "\")
   For i = 0 To UBound(vArray)
         MsgBox vArray(i)
   Next i
End Sub
---------------------------------

If you need only the last component:

--------------------------------
Private Sub Dir1_Change()

Dim vArray
Dim i As Integer

vArray = Split(Dir1.Path, "\")

    MsgBox vArray(UBound(vArray))
   
End Sub
----------------------------------
0
 

Expert Comment

by:vbhelp12345
ID: 9795537
here is it is

Dim dir1 As String

Private Sub Command1_Click()
Dim i As Integer
Dim lent As Integer
Dim j As Integer
Dim varname As String
lent = Len(dir1)
i = 1
Do While i < lent
If Mid(dir1, i, 1) = "\" Then
  j = i
End If
i = i + 1
Loop
varname = Mid(dir1, j + 1, (lent - j) - 1)
MsgBox varname
End Sub

Private Sub Form_Load()
dir1 = "C:\Documents and settings\all users\Desktop\"
0
 

Author Comment

by:Shibjuh
ID: 9795570
Thanks guys :) very fast replies. Sorry vbhelp12345 for not giving you points but dbrckovi's answer was shorter
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9795584
Thanx!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now