Windows Vista Forums

VBScript to list PST files and their location on a specific drive ona PC to a CSV file
  1. #1


    anthony.schemel Guest

    VBScript to list PST files and their location on a specific drive ona PC to a CSV file

    Hi guys

    I need to list all the PST files on a server and for convenience put
    them in a CSV or TXT file. I need their locations as well. Now, I
    have run a few scripts to get this done but get errors when running
    them. The strange thing is that it runs perfectly on my own PC.

    The first one gives me a 'Remote Procedure Call' error and the code is
    as follows:

    Dim fso, NewFile

    Set fso = CreateObject("Scripting.FileSystemObject")

    strComputer = "00172FPSJNB0007"

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

    Set colFiles = objWMIService.ExecQuery _
    ("Select Name from CIM_DataFile" _
    & " where Drive='d:' and Extension='pst'")

    Set NewFile = fso.CreateTextFile("c:00172FPSJNB0007.csv", True)

    For Each objFile in colFiles
    NewFile.WriteLine strOffset & objFile.Name
    Next

    NewFile.close

    Wscript.Echo "Done."
    Wscript.Quit

    Now, the next one actually lists all files on the PC. That is fine
    because one can actually then afterwards pick out the details you are
    looking for. After it generates about a 1000 entries in the CSV file
    it through out an error:

    Dim sFolder, fso, NewFile

    strComputer = "00172FPSJNB0007"
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
    \cimv2")

    Set fso = CreateObject("Scripting.FileSystemObject")

    strFolderName = "d:\users"

    Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} "
    _
    & "Where AssocClass = Win32_Subdirectory " _
    & "ResultRole = PartComponent")

    arrFolderPath = Split(strFolderName, "\")
    strNewPath = ""
    For i = 1 to Ubound(arrFolderPath)
    strNewPath = strNewPath & "\\" & arrFolderPath(i)
    Next
    strPath = strNewPath & "\\"

    Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile where Path = '" & strPath & "'")

    Set NewFile = fso.CreateTextFile("c:00172FPSJNB0007a.csv", True)

    For Each objFile in colFiles
    NewFile.WriteLine strOffset & objFile.Name
    Next

    For Each objFolder in colSubfolders
    GetSubFolders strFolderName
    Next

    Sub GetSubFolders(strFolderName)
    On Error Resume Next
    Set colSubfolders2 = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName &
    "'} " _
    & "Where AssocClass = Win32_Subdirectory " _
    & "ResultRole = PartComponent")

    For Each objFolder2 in colSubfolders2
    strFolderName = objFolder2.Name
    arrFolderPath = Split(strFolderName, "\")
    strNewPath = ""
    For i = 1 to Ubound(arrFolderPath)
    strNewPath = strNewPath & "\\" & arrFolderPath(i)
    Next
    strPath = strNewPath & "\\"

    Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile where Path = '" & strPath &
    "'")

    For Each objFile in colFiles
    NewFile.WriteLine strOffset & objFile.Name
    Next

    GetSubFolders strFolderName
    Next
    On Error GoTo 0
    End Sub

    Wscript.Echo "Done"

    Any ideas guys?



      My System SpecsSystem Spec

  2. #2


    Pegasus \(MVP\) Guest

    Re: VBScript to list PST files and their location on a specific drive on a PC to a CSV file


    <anthony.schemel@xxxxxx> wrote in message
    news:892c7529-1883-47a2-99b3-205b942f39c5@xxxxxx

    > Hi guys
    >
    > I need to list all the PST files on a server and for convenience put
    > them in a CSV or TXT file. I need their locations as well. Now, I
    > have run a few scripts to get this done but get errors when running
    > them. The strange thing is that it runs perfectly on my own PC.
    >
    > The first one gives me a 'Remote Procedure Call' error and the code is
    > as follows:
    >
    > Dim fso, NewFile
    >
    > Set fso = CreateObject("Scripting.FileSystemObject")
    >
    > strComputer = "00172FPSJNB0007"
    >
    > Set objWMIService = GetObject("winmgmts:" _
    > & "{impersonationLevel=impersonate}\\" & strComputer & "\root
    > \cimv2")
    >
    > Set colFiles = objWMIService.ExecQuery _
    > ("Select Name from CIM_DataFile" _
    > & " where Drive='d:' and Extension='pst'")
    >
    > Set NewFile = fso.CreateTextFile("c:00172FPSJNB0007.csv", True)
    >
    > For Each objFile in colFiles
    > NewFile.WriteLine strOffset & objFile.Name
    > Next
    >
    > NewFile.close
    >
    > Wscript.Echo "Done."
    > Wscript.Quit
    >
    > Now, the next one actually lists all files on the PC. That is fine
    > because one can actually then afterwards pick out the details you are
    > looking for. After it generates about a 1000 entries in the CSV file
    > it through out an error:
    >
    > Dim sFolder, fso, NewFile
    >
    > strComputer = "00172FPSJNB0007"
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
    > \cimv2")
    >
    > Set fso = CreateObject("Scripting.FileSystemObject")
    >
    > strFolderName = "d:\users"
    >
    > Set colSubfolders = objWMIService.ExecQuery _
    > ("Associators of {Win32_Directory.Name='" & strFolderName & "'} "
    > _
    > & "Where AssocClass = Win32_Subdirectory " _
    > & "ResultRole = PartComponent")
    >
    > arrFolderPath = Split(strFolderName, "\")
    > strNewPath = ""
    > For i = 1 to Ubound(arrFolderPath)
    > strNewPath = strNewPath & "\\" & arrFolderPath(i)
    > Next
    > strPath = strNewPath & "\\"
    >
    > Set colFiles = objWMIService.ExecQuery _
    > ("Select * from CIM_DataFile where Path = '" & strPath & "'")
    >
    > Set NewFile = fso.CreateTextFile("c:00172FPSJNB0007a.csv", True)
    >
    > For Each objFile in colFiles
    > NewFile.WriteLine strOffset & objFile.Name
    > Next
    >
    > For Each objFolder in colSubfolders
    > GetSubFolders strFolderName
    > Next
    >
    > Sub GetSubFolders(strFolderName)
    > On Error Resume Next
    > Set colSubfolders2 = objWMIService.ExecQuery _
    > ("Associators of {Win32_Directory.Name='" & strFolderName &
    > "'} " _
    > & "Where AssocClass = Win32_Subdirectory " _
    > & "ResultRole = PartComponent")
    >
    > For Each objFolder2 in colSubfolders2
    > strFolderName = objFolder2.Name
    > arrFolderPath = Split(strFolderName, "\")
    > strNewPath = ""
    > For i = 1 to Ubound(arrFolderPath)
    > strNewPath = strNewPath & "\\" & arrFolderPath(i)
    > Next
    > strPath = strNewPath & "\\"
    >
    > Set colFiles = objWMIService.ExecQuery _
    > ("Select * from CIM_DataFile where Path = '" & strPath &
    > "'")
    >
    > For Each objFile in colFiles
    > NewFile.WriteLine strOffset & objFile.Name
    > Next
    >
    > GetSubFolders strFolderName
    > Next
    > On Error GoTo 0
    > End Sub
    >
    > Wscript.Echo "Done"
    >
    > Any ideas guys?
    Seeing that this is a standard maintenance task for which
    there are standard maintenance tools, I would take the
    easy way out and type this command:

    dir /s /b \\00172FPSJNB0007\d$\*.pst > c:\PSTFiles.txt

    Very fast to write, easy to maintain and fast to execute.



      My System SpecsSystem Spec

VBScript to list PST files and their location on a specific drive ona PC to a CSV file problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows offline files --> only specific file types Daniel Lüngen Vista General 0 08 Sep 2009
File Searches: Can I Search for MP3 Files Via a Specific Bitrate? T. Hulot Vista music pictures video 1 13 Jun 2009
File Searches: Can I Search for MP3 Files Via a Specific Bitrate? T. Hulot Vista General 1 13 Jun 2009
Can one put offline files at a specific location? Michael Moser Vista file management 0 06 Dec 2007
how to remove a file location from drop down browse list? dropdown Vista file management 1 18 Mar 2007