Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS to spreadsheet of folders subfolders subsubfolders (and so on......) and the files listed inside

Status
Not open for further replies.

marthy99

Technical User
May 28, 2015
3
NL
this is what i have can someone modify it ? or guide me i am a real noob didnt do anything with vbs for a long time

objStartFolder = "C:\Folder\"



Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

objExcel.Workbooks.Add

intRow = 2



objExcel.Cells(1, 1).Value = "Folder"

objExcel.Cells(1, 2).Value = "File Name"



Set objFSO = CreateObject

("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(objStartFolder)



Set colFiles = objFolder.Files

For Each objFile in colFiles

objExcel.Cells(intRow, 1).Value = objfolder.Name

objExcel.Cells(intRow, 2).Value = objFile.Name

intRow = intRow + 1

Next



ShowSubfolders objFSO.GetFolder(objStartFolder)



Sub ShowSubFolders(Folder)

For Each Subfolder in Folder.SubFolders

objExcel.Cells(intRow, 1).Value = Subfolder.Path



Set objFolder = objFSO.GetFolder(Subfolder.Path)

Set colFiles = objFolder.Files

For Each objFile in colFiles

objExcel.Cells(intRow, 2).Value = objFile.Name

intRow = intRow + 1

Next



ShowSubFolders Subfolder

Next

End Sub



objExcel.Range("A1:B1").Select

objExcel.Selection.Interior.ColorIndex = 19

objExcel.Selection.Font.ColorIndex = 11

objExcel.Selection.Font.Bold = True

objExcel.Cells.EntireColumn.AutoFit



MsgBox "Done
 
So it looks like you are trying to create a spreadsheet of every subfolder and every file under a specific path. The logic of the code looks OK, what isn't happening that you want to? I ran this script against my temp folder and it worked fine.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
wekk yes that is what i tryed but the problem is that the rootfolsder used (called folder) is added and the first layer of folders in there but not the folders in the second layer (sorry for my english is not that good)
 
OK, so what you are trying to do is called a recursive listing. Here is an example:
Code:
Set objFSO = CreateObject("Scripting.Filesystemobject")
StartFolder = "C:\Windows"

Set oRoot = objFSO.GetFolder(StartFolder)
WScript.Echo oRoot.Name

For Each oFile In oRoot.Files
	WScript.Echo vbTab & vbTab & oFile.Name
Next

For Each oSubFolder In oRoot.SubFolders
	WScript.Echo oSubFolder.Name
	GetSubInfo oSubFolder.Path
Next



Function GetSubInfo(oSubFolder)
	Set oFolder = objFSO.GetFolder(oSubFolder)
	For Each oFile In oFolder.Files
		WScript.Echo vbTab & vbTab & oFile.Name
	Next
	For Each oSub In oFolder.SubFolders
		WScript.Echo oSub.Name
		GetSubInfo oSub.Path
	Next
End Function

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top