VBScript function to get folder owner

This VBScript function parses the output of the DIR /Q command to find the owner of the specified folder. It demonstrates using a WScript.Shell Exec object to capture the output from a command, then searching within the output for certain text strings.

Function folderOwner(sFolder)
  Dim oShell, oFSO, oExec, sText, iStart, iEnd
  Set oShell = CreateObject("WScript.Shell")
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  If oFSO.FolderExists(sFolder) Then
   Set oExec = oShell.Exec("cmd /c dir /q " & sFolder & " | find /i ""<dir>""")
   Do While oExec.StdOut.AtEndOfStream <> True
    sText = oExec.StdOut.ReadAll
   iEnd = InStr(sText,"."&vbCrLf)
   iStart = InStrRev(sText,">",InStr(sText,"."&vbCrLf)) + 1
   If iEnd>iStart Then
    folderOwner = Trim(Mid(sText,iStart,iEnd-iStart))
    folderOwner = "Unable to determine"
   End If
   folderOwner = "Folder not found"
  End If
 End Function


WScript.Echo "Owner of C:\Users\RCMTech is " & folderOwner("C:\Users\RCMTech")
This entry was posted in Scripting, Windows and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.