Solved

Just the dir name not the whole path

Posted on 2003-11-21
4
266 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

830 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