By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Returning subfolder name with path name

P: 9
I am using the following code

Sub Ck()

Dim strStartPath As String

strStartPath = "d:\workpack\rra"
ListFolder strStartPath

End Sub
Sub ListFolder(sFolderPath As String)

Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim i As Integer

Set FSfolder = FS.GetFolder(sFolderPath)

For Each subfolder In FSfolder.SubFolders
DoEvents
i = i + 1
'added this line
Cells(i, 1) = subfolder
'commented out this one
'Debug.Print subfolder
Next subfolder

Set FSfolder = Nothing


End Sub

to return the subfolder names into cells in excel, problem is this also returns the path (ie D:\workpack\rra\example1), I only want the subfolder name, how can I do this?
Jan 18 '08 #1
Share this Question
Share on Google+
5 Replies


kadghar
Expert 100+
P: 1,295
I am using the following code

(...)

Sub ListFolder(sFolderPath As String)

(...)

to return the subfolder names into cells in excel, problem is this also returns the path (ie D:\workpack\rra\example1), I only want the subfolder name, how can I do this?
the easiest way, since you have sFolderPath:
cells(i,1) = mid(subfolder, len(sFolderPath)+1)

HTH
Jan 18 '08 #2

P: 9
I have no idea what that means but it did the trick so thanks
Jan 18 '08 #3

kadghar
Expert 100+
P: 1,295
I have no idea what that means but it did the trick so thanks
mid will return a substring from the original string, so if you have

str1 = "hello world"

mid(str1, 2,4) = "ello" where the first parameter is the string, the second is the starting position and the third is the length. The third one is optional, and will give you the rest of the string as default.

HTH
Jan 18 '08 #4

P: 58
Try...
Cells(i, 1) = subfolder.Name


I am using the following code
-snip-
to return the subfolder names into cells in excel, problem is this also returns the path (ie D:\workpack\rra\example1), I only want the subfolder name, how can I do this?
Jan 19 '08 #5

P: 58
Try...
Cells(i, 1) = subfolder.Name

I am using the following code
-snip-
to return the subfolder names into cells in excel, problem is this also returns the path (ie D:\workpack\rra\example1), I only want the subfolder name, how can I do this?
Jan 19 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.