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

List all files in folder & subfolders

Status
Not open for further replies.

biglebowski

Technical User
Jan 29, 2004
3,117
GB
Is there a way of changing this script so that it will output the name of each file to a text or csv file?

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Scripts'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFileList
Wscript.Echo objFile.Name
Next

I know nothing about scripting and found this on the MS scripting site.

Thanks

When I was born I was so suprised I didn't talk for 18 months
 
Single directory [cmd /c dir c:\scripts > C:\someoutput.txt]

Code:
strComputer = "."
Const ForAppending = 2
LogFile = "c:\backup\exportme.log"
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile:Set objLogFile = objFSO.CreateTextFile(logfile, 2, True)


Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Scripts'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In colFileList
    objLogFile.Write objFile.Name
	objLogFile.Writeline
Next

objLogFile.Close

Include subdirectories [not pretty code but it works]
or DOS would be [cmd /c dir c:\scripts /s > C:\someoutput.txt]
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 2
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")

LogFile = "c:\backup\exportme.log"
Dim objLogFile:Set objLogFile = objFSO.CreateTextFile(logfile, 2, True)

objStartFolder = "C:\Scripts"

Set objFolder = objFSO.GetFolder(objStartFolder)
objLogFile.Write objFolder.Path
objLogFile.Writeline
Set colFiles = objFolder.Files
For Each objFile in colFiles
    objLogFile.Write objFile.Name
	objLogFile.Writeline
Next


ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        objLogFile.Write Subfolder.Path
		objLogFile.Writeline
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            objLogFile.Write objFile.Name
			objLogFile.Writeline
        Next
        ShowSubFolders Subfolder
    Next
End Sub

objLogFile.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top