Hi all I'm hoping for pointers or explanation as to why my attempt at a script might be failing please :)
I'm relatively new to VB and through some searching etc have managed to cobble together the script below, basically I'm trying to search across drives on all pc's for any and all .pst or .ost files whether they may be on a single drive or across multiple drives (C:, D:, etc.) from there I'm outputting the query results to a newly created .txt file so that an engineer can check that all of the files have been copied when the script terminates, and as I say once a file is discovered I'm copying this to a location of the root of C:
Where I'm failing at the moment is with duplicate file names, my .txt file for instance is showing .pst files with duplicate names in different areas on the users drive...my script currently only seems to be copying the first .pst file discovered so if there are 2 files named usera.pst I only seem to be able to copy one of them even though my .txt file correctly shows them both and their locations?
I've tried to append an index number to the files but I'm not having any success at present and know I'm probably missing something fundemental in my thinking so any advice is gratefully received!
As I say I'm happy for any of you to advise and suggest improvements if you feel what I have isn't efficient or if I'm going about it completely wrong but I'd really like a pointer as to what I may be doing wrong in terms of trying to handle the duplicate file names.
Many thanks in advance!
I'm relatively new to VB and through some searching etc have managed to cobble together the script below, basically I'm trying to search across drives on all pc's for any and all .pst or .ost files whether they may be on a single drive or across multiple drives (C:, D:, etc.) from there I'm outputting the query results to a newly created .txt file so that an engineer can check that all of the files have been copied when the script terminates, and as I say once a file is discovered I'm copying this to a location of the root of C:
Where I'm failing at the moment is with duplicate file names, my .txt file for instance is showing .pst files with duplicate names in different areas on the users drive...my script currently only seems to be copying the first .pst file discovered so if there are 2 files named usera.pst I only seem to be able to copy one of them even though my .txt file correctly shows them both and their locations?
I've tried to append an index number to the files but I'm not having any success at present and know I'm probably missing something fundemental in my thinking so any advice is gratefully received!
Code:
On Error Resume Next
Const CONVERSION_FACTOR = 1048576
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where Extension = 'pst' OR Extension = 'ost'")
If colFiles.Count = 0 Then
msgbox "No .pst files detected...please search manually!"
Wscript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder "C:\PST_BACKUP"
Set objTextFile = objFSO.CreateTextFile("C:\PST_BACKUP\PST_Data.txt")
For Each objFile in colFiles
If Err = 0 Then
searchFileSize = objFile.FileSize / CONVERSION_FACTOR
objTextFile.Writeline(objFile.Name & " Size: " & Int(searchFileSize) & "MB")
objTextFile.Writeline("*****************************************************")
objTextFile.Writeline("")
i = 1
strCopy = "C:\PST_BACKUP\" & objFile.FileName & i & "_BK" _
& "." & objFile.Extension
i = i + 1
objFile.Copy(strCopy)
Else If Err <> 0 Then
WScript.Echo "Error collecting data: " & Err.Description
Wscript.Quit
End If
End If
Next
objTextFile.CloseMany thanks in advance!