Hello,

I'm trying to create a script that searches for all .pst files on a local pc and gathers the filename, path and size and outputs that data to a file called username.txt. I am having problems with my code. It creates the username.txt but does not write the .pst file information. Please help!

strComputer = "."
Const ForAppending = 2

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

Set colItems = objWMIService.ExecQuery("Select Username From Win32_ComputerSystem")

For Each objItem in colItems
    arrName = Split(objItem.UserName, "\")
    
Next

Set colItems = objWMIService.ExecQuery _
  ("Select * from CIM_DataFile Where Extension = 'pst' AND Drive = 'C:'")
 
If colItems.Count = 0 Then
    Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Script\" & arrName(1) & ".txt", ForAppending, True)

  For each ObjItem in colItems
    objFile.Write(arrName(1)& ",")
    objFile.Write(objFile.Drive & objFile.Path & ",")
    objFile.Write(objFile.FileName & "." & objFile.Extension & ",")
    objFile.Write(objFile.FileSize & vbCrLf)

Next
 objFile.Close

Dani AI

Generated

The symptom reported by (file created but no PST lines) matches the specific bug called out: the output stream is being queried for file properties instead of the WMI result objects. Other common pitfalls in this script were the wrong append constant, brittle username extraction, and no check that the output folder exists. The example below shows a concise, robust VBScript pattern that pulls properties from each WMI item, gets a sensible username via WScript.Network, ensures the folder exists, and appends CSV lines.

Const ForAppending = 8

Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.UserName
If userName = "" Then userName = "UnknownUser"

Set objFSO = CreateObject("Scripting.FileSystemObject")
outDir = "C:\Script"
If Not objFSO.FolderExists(outDir) Then objFSO.CreateFolder(outDir)
outFile = outDir & "\" & userName & ".txt"

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colFiles = objWMI.ExecQuery("SELECT Name, FileSize FROM CIM_DataFile WHERE Extension='pst' AND Drive='C:'")

Set outTS = objFSO.OpenTextFile(outFile, ForAppending, True)
For Each f In colFiles
    outTS.WriteLine userName & "," & f.Name & "," & CStr(f.FileSize)
Next
outTS.Close

Notes: WMI's CIM_DataFile Name gives the full path and FileSize is numeric. Scanning an entire drive via WMI can be slow; for large drives a recursive FileSystemObject folder walk is often faster. For authoritative field names and behavior see CIM_DataFile on Microsoft Docs. ’s question about VB vs VBScript is relevant: the snippet above is VBScript intended for WSH.

Recommended Answers

All 2 Replies

Do you use Visual Basic? or just VBScript??

objFile is your TextStream object. You need to pull the properties off of the Enumerated ObjItem object. objFile.Write([U]objItem[/U].Drive...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.