• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Get folder paths vba

Hello Friends.

I need to get folder paths in a given folder with all the file names.

I really appreciate for your quick help.
 
Hello Arpana.

Try this.
Code:
Sub Get_folder_paths_vba()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("C:\Users\MONTY\Desktop\New") 'Change the path here
i = 1
For Each objSubFolder In objFolder.subfolders

Cells(i + 1, 1) = objSubFolder.Name

Cells(i + 1, 2) = objSubFolder.Path
i = i + 1
Next objSubFolder
End Sub
 
Monty it works.

Can i have little modification, may be i asked question wrong need file paths

it gives folder paths,Am sorry for confusion guys.
 
Try this.

Code:
Sub Get_folder_File_path_vba()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String

strDirectory = "C:\Users\MONTY\Desktop\" 'Change Path here
i = 1
flag = True
varDirectory = Dir(strDirectory, vbNormal)

While flag = True
If varDirectory = "" Then
flag = False
Else
Cells(i + 1, 1) = varDirectory
Cells(i + 1, 2) = strDirectory + varDirectory

varDirectory = Dir
i = i + 1
End If
Wend
End Sub
 
It works

One more Quick question and sorry to bother you.
Wanted a button which will browse the folder on selection it should get all the files with path

Example

Colum A Column B

File name File Path

Thank you for your efforts.
 
Try this.

Code:
Sub Get_folder_file_path()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer
Dim flder As FileDialog
Dim foldername As String

Set flder = Application.FileDialog(msoFileDialogFolderPicker)
foldername = flder.Show

foldername = flder.SelectedItems(1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFolder = objFSO.GetFolder("C:\Users\MONTY\Desktop\")
Set objFolder = objFSO.GetFolder(foldername)
i = 1

For Each objSubFolder In objFolder.subfolders

Cells(i + 1, 1) = objSubFolder.Name

Cells(i + 1, 2) = objSubFolder.Path
i = i + 1
Next objSubFolder
End Sub
 
Works well

A complete package for me now with one last query on the mentioned code.

instead of giving path in the code. can we make it dynamic.:(

Still to go with many more questions as i struck with.:(:(:(:(
 
Hello Arpana.

Did you test the code provided.

Earlier code was not dynamic now the code is dynamic on click of a button open's a dialog box and you can select folder.

Please accept my piece of advise when somebody provides you a code try understand it before you test it that how am doing to learn form our experts, Still confusion forum is ready to take your questions. Happy learning..All the best..Waiting for more interesting questions from you.

i have commented in the above code.
'Set objFolder = objFSO.GetFolder("C:\Users\MONTY\Desktop\")
Set objFolder = objFSO.GetFolder(foldername)

Let me know if i confused you.
 
Back
Top