Option Explicit
'On Error Resume Next
Dim refWMI
Dim strQuery
Dim colOwner
Dim refItem
Dim FSO
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists ("C:\FileOwner.txt") then
FSO.DeleteFile "C:\FileOwner.txt"
End If
Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)
Do Until txtFileName.atendofstream=true
set refWMI = GetObject("winMgmts:\\")
filespec=txtFileName.readline
strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
"'" & filespec & "'} WHERE " & _
"AssocClass=Win32_LogicalFileOwner " & _
"ResultRole=Owner"
set colOwner = refWMI.ExecQuery(strQuery)
For Each refItem In colOwner
If Err = 0 Then
txtFileOwner.writeline filespec & " is owned by " & refItem.AccountName
End If
Next
Set colOwner = Nothing
set refWMI = Nothing
loop