# queries to find pst



## perahlgren (Aug 28, 2012)

I found this script here. 
Can i make this script only find PST files?

Thanks?

.

'---------------------------------------------------------------
'-- Dan McCracken 9/25/2008----------------------------------------
'---This is version used 5/20/2009 to read ACES folder'
'--Writes the properties and attributes of a files to tab text file. 
'--Sub Routine "ShowSubFolders" allows it to traverse thru folder tree
'--Sub Routine "GetFileOwner" finds the owner (security) of the file
' ---------------------------------------------------------------
'for gettng file owner
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'set up for write file
Const ForAppending = 8
Const ForWriting = 2
Public strFileOwner 'get owner sub routine'
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("h:\afiles\ACES_03Mar2010.txt", ForWriting, True) 'Output file here<---'
'enumerate file props
objStartFolder = "P:\Public\OPPD\ES\ACES" 'Folder name here<----
'objStartFolder = "C:\Documents and Settings" 'Folder name here<-----

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo "Starting Here - " & objFolder.Path
'--------------Write the tab headers-must be insync with output---
objTextFile.WriteLine ("File_Name" & vbTab & "File_Size"& vbTab & "File_Owner" _
& vbTab & "Path" & vbTab & "Last_Access" & vbTab & "Modified" _
& vbTab & "Created")

Set colFiles = objFolder.Files
For Each objFile in colFiles

'for file owner
strFile = objFSO.GetAbsolutePathName(objFile)
GetFileOwner(strFile) '----------call subroutine'

'write the file info' 
objTextFile.WriteLine (objFile.Name & vbTab & objFile.Size & vbTab _ 
& strFileOwner & vbTab & objFSO.GetAbsolutePathName(objFile)& vbTab _
& objFile.DateLastAccessed & vbTab & objFile.DateLastModified _
& vbTab & objFile.DateCreated)
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)
objTextFile.Close
Wscript.Echo "Wow! It Finnished - " & now()

'---------------SubRoutines---------------------------------------
'------------Spin thru Sub folders--------------------------------
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
' Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
'for file owner
strFile = objFSO.GetAbsolutePathName(objFile)
GetFileOwner(strFile) '----------call subroutine---------
'Wscript.Echo objFile.Name & "/" & strFileOwner
'write the file info' 
objTextFile.WriteLine (objFile.Name & vbTab & objFile.Size & vbTab _ 
& strFileOwner & vbTab & objFSO.GetAbsolutePathName(objFile)& vbTab _
& objFile.DateLastAccessed & vbTab & objFile.DateLastModified _
& vbTab & objFile.DateCreated)

Next
' Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub
'-----------------Get the File Owner-------------------------------
Sub GetFileOwner(strFile)
If IsNull(strFile) or IsEmpty(strFile) Then 
strFileOwner = "Null Value"
Else
Set colItems = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _
& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
For Each objItem in colItems
' Wscript.Echo objFile.Name & " " & objItem.AccountName
strFileOwner = objItem.AccountName 
Next
End If 
End Sub


----------



## draceplace (Jun 8, 2001)

Hey thats my script!! Welcome to TSG...come back and help others when you get a chance.

Have you ran this script? I recommend commenting out the call tho 'GetFileOwner' unless you really want that. Some systems don't like it or it really slows down. Depending how big your folder structure is it may be slow anyway...Test on small folder structure before unleashing it on a big job.

To get pst only...add the Bold code below and also add it to the 'ShowSubFolder' routine in the same way.

Set colFiles = objFolder.Files
For Each objFile in colFiles

*If inStr(objFile.Name,".pst") Then *
'for file owner
strFile = objFSO.GetAbsolutePathName(objFile)
GetFileOwner(strFile) '----------call subroutine'

'write the file info' 
objTextFile.WriteLine (objFile.Name & vbTab & objFile.Size & vbTab _ 
& strFileOwner & vbTab & objFSO.GetAbsolutePathName(objFile)& vbTab _
& objFile.DateLastAccessed & vbTab & objFile.DateLastModified _
& vbTab & objFile.DateCreated)

*End if*
Next


----------



## perahlgren (Aug 28, 2012)

I have many folders i want to exclude, So i treid to mke a txt file with all names and changed the script but i didn´t work as i hoped. Please help me (the updates is marked with Bold

'On Error Resume Next
'for gettng file owner
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'set up for write file
'Const ForAppending = 8
*Const ForReading = 1*
Const ForWriting = 2

Public strFileOwner 'get owner sub routine'
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("d:\pst\allpst3.txt", ForWriting, True) 'Output file here<---'
*Set objFolderFile = objFSO.OpenTextFile ("d:\pst\folders.txt", ForReading, 1)*
'enumerate file props
objStartFolder = "\\bronas01\Home"'Folder name here<----

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo "Starting Here - " & objFolder.Path
'--------------Write the tab headers-must be insync with output---
objTextFile.WriteLine ("File_Name" & vbTab & "File_Size" _
& vbTab & "Path" & vbTab & "Last_Access" & vbTab & "Modified" _
& vbTab & "Created")

Set colFiles = objFolder.Files
For Each objFile in colFiles

'for file owner
'trFile = objFSO.GetAbsolutePathName(objFile)
'GetFileOwner(strFile) '----------call subroutine'

'write the file info' 
objTextFile.WriteLine (objFile.Name & vbTab & objFile.Size & vbTab _ 
& objFSO.GetAbsolutePathName(objFile)& vbTab _
& objFile.DateLastAccessed & vbTab & objFile.DateLastModified _
& vbTab & objFile.DateCreated)
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)
objTextFile.Close
Wscript.Echo "Wow! It Finnished - " & now()

'---------------SubRoutines---------------------------------------
'------------Spin thru Sub folders--------------------------------

Sub ShowSubFolders(Folder)
strFolderName = objFolderFile.ReadLine
*IF objFolder.name <> strFolderName Then*

For Each Subfolder in Folder.SubFolders
' Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If inStr(objFile.Name,".pst") Then 
'for file owner
strFile = objFSO.GetAbsolutePathName(objFile)
'GetFileOwner(strFile) '----------call subroutine'

'write the file info' 
objTextFile.WriteLine (objFile.Name & vbTab & objFile.Size & vbTab _ 
& strFileOwner & vbTab & objFSO.GetAbsolutePathName(objFile)& vbTab _
& objFile.DateLastAccessed & vbTab & objFile.DateLastModified _
& vbTab & objFile.DateCreated)

End if
Next
' Wscript.Echo
ShowSubFolders Subfolder
Next
End if
End Sub


----------



## draceplace (Jun 8, 2001)

To exclude folders one way to do it would be to build an array/table or a dictionary from your exclude listing file. Open the text file load the dictionary (close the text file) then test for exists at the folder level.

The way your code is now you would need to open the exclusion file and spin the whole file each time the folder changes

Another way would be to let go through all the folders then remove them in access or excel after the fact. If there are no PSTs there will be nothing to remove. It will just take longer as it churns thru folders that you don't want.


----------

