Help with vbs to loop thu folders

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi

I realise that this isn't strictly Excel, but I have seen vbs been used before so I'm hoping someone can assist. I have a problem with following code... It loops thru all subfolders correctly but I want to return the properties of the files housed within the folders.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objLogFile = objFSO.CreateTextFile("C:\ScriptLog.txt")

LoopSubFolders objFSO.GetFolder("H:\")

Sub LoopSubFolders(Folder)

	For Each SubFolder in Folder.SubFolders
		LoopSubFolders SubFolder

			If SubFolder.Size > 10240 Then
				For Each objFile in Folder.SubFolders
					objLogFile.WriteLine objFile.Type
				Next
			End If

	Next

End Sub

I think the problem is here:
Code:
For Each objFile in Folder.SubFolders

I have noticed that this still only looks at File Folders and not the actuals files (e.g. .xls, .pdf etc.)

Any help? :)

Thanks
Jon
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello Jon

Don't you need to access an actual Files collection? I have bugger all familiarity with VBS so could be way wrong here but I would have thought the following may improve things:

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject") 

Set objLogFile = objFSO.CreateTextFile("C:\ScriptLog.txt") 

LoopSubFolders objFSO.GetFolder("H:\") 

Sub LoopSubFolders(Folder) 

   For Each SubFolder in Folder.SubFolders 
      LoopSubFolders SubFolder 

         If SubFolder.Size > 10240 Then 
            For Each objFile in Folder.SubFolders.Files
               objLogFile.WriteLine objFile.Type 
            Next 
         End If 

   Next 

End Sub
 
Upvote 0
Jon

That isn't really VBS, it's VBA using the FileSystemObject.

Like Richard I'm not too familiar with this sort of thing, perhaps a tutorial like this might help you.:)
 
Upvote 0
That isn't really VBS, it's VBA using the FileSystemObject.

It's written in notepad and saved as .vbs. I've got scriptcentre installed and I've been referencing that for the code. I'm almost there.

Norie, thanks for the tutorial, I'm going to have a browse thru it. :biggrin:

Rich, I tried that but it bugs out on .Files... Darn it!!! :devilish:

Thanks guys...
 
Upvote 0
Ooooooooooh Rich, you were so nearly there mate. :biggrin:

Code:
For Each objFile in SubFolder.Files

This works! Thanks for the suggestion, it got me there! Next shout is on me!
(y)

Norie, thanks for this . It's wicked!!! :biggrin: Given me nice grounding for use in VBA...
 
Upvote 0
Jon

No problem.:)

I honestly did think that was VBA you'd posted.:)

It's been a long time since I scripted in VBS.:eek:

And that was just simple stuff like message boxes and arrays to assist with data input.
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,260
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top