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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there an easier way of displaying contents of a folder? 3

Status
Not open for further replies.

barny2006

MIS
Mar 30, 2006
521
US
hi,
currently, i'm using a dos command in vbs to put contents of a folder in a text file, then opening it, reading it and displaying it. is there an easier way of doing this without writinig it to a file?
the code that i have is:
Code:
Set wshshell = CreateObject("WScript.Shell")     
wshshell.Run("cmd.exe /c dir /b c:\my_folder\*.* > c:\test.txt")            
ForReading = 1 
file_name = "c:\test.txt"    
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(file_name, ForReading)
Do Until objFile.AtEndOfStream
   content = content & objFile.ReadLine & vbcrlf         
Loop
msgbox content
thanks for the help.
 
Of course, use the same Scripting.FileSystemObject object to loot at the folder and the files it contains.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Option Explicit
'On Error Resume Next

Dim objFSO, objFolder, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp")

For Each objFile In objFolder.Files
WScript.Echo objFile.Name
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists("c:\test") Then
Set objFolder = objFSO.GetFolder("c:\test")
For Each aFile In objFolder.Files
Wscript.Echo aFile.Name
Next
For Each aFolder In objFolder.SubFolders
Wscript.Echo aFolder.Name
Next
Set objFolder = Nothing
End If
Set objFSO = Nothing
 
thanks so much, dm.
i think that will work good.
appreciate it.
 
mrmovie,
i have a question:
would this also print subfolders inside a folder? if it does, then it may work better.
 
If you want files within subfolders as well you can use a function to recurse through them.

Code:
GetFileNames "c:\temp"

Function GetFileNames(strPath)
' 	On Error Resume Next
	
	Dim objFSO, objFolder, objFile, objSubFolder
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strPath)
	
	For Each objFile In objFolder.Files
		WScript.Echo objFile.Name
	Next
	
	For Each objSubFolder In objFolder.SubFolders
' 		WScript.Echo objSubFolder.Name
		GetFileNames objSubFolder.Path
	Next
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
FileSystemObject is about as slow as you can get at folder recursion, try c# if you are looking at large amounts of data
 
thanks a lot dm, mrmovie.
all the codes and suggestions were really helpful. i will have my code done right.
thanks again.
 
i am sure you dont mind dm4ever, strictly speaking your function should be a sub and perhaps code can be shorten not sure of the speed impacts of passing the object rather than the path string though (this post might bite me :))

strDir = "f:\"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
'wscript.Echo aItem.Name
If LCase(Right(Cstr(aItem.Name), 3)) = "bak" Then
'do file manip, copy delete here
End If
Next

For Each aItem In pCurrentDir.SubFolders
'wscript.Echo aItem.Name & " passing recursively"
getInfo(aItem)
Next

End Sub
 
getInfo(aItem)
With this this call method you don't pass the object but its default value.
Use either:
Call getInfo(aItem)
or:
getInfo aItem

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top