A little help with an old script - PST migration - Part Two: Changing the file name output
-
Wednesday, December 05, 2012 1:56 PM
So I have the script working the way I want mostly but it was brought to my attention that realistically we should probably make the file name something shorter, simpler, and easier to use.
Ideal File name would be something like: <current user>_<driveletter>_archive<#>.pst
i.e.: ddonowitz_c_archive1.pstRight now the file name that is being output by the script is very ... long and while it includes a machine name it would be much better to grab a username.
I found Set WshNetwork = WScript.CreateObject("WScript.Network") which seems to work and grab the username properly but I can't figure out where/how to place that to make it part of the file name or if that is even possible.
Is there a way to make this happen?
Current Code:
dim fso set fso = CreateObject("Scripting.FileSystemObject") dim wshShell Set wshShell = WScript.CreateObject("WScript.Shell") Dim strDestinationFolder, strDestinationPath '***** Edit here ****** strDestinationFolder = "\\<SERVERNAME>\<SHARED FOLDER NAME>\" If Not fso.FolderExists(strDestinationFolder) Then MsgBox strDestinationFolder & " not found!",vbCritical + vbOKOnly,"Error!" WScript.Quit End If 'Get computer name strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") strComputer = UCase(strComputer) 'use WMI to find PST files. Really slow... Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile" _ & " WHERE (Drive='C:') AND Extension='pst'") i = 0 For Each file in colFiles i = i + 1 strFile = File.Name 'CompName + UserName + archive (count).pst 'strOwner = FileOwner(strFile) 'StrOwner = Replace(strOwner,"\","_") 'strDestinationPath = strDestinationFolder & "\" & strComputer & "_" & strOwner & "_archive" & i & ".pst" 'or 'CompName + Domain + OriginalPathName + archive.pst (count +1), i.e. JOHNNYSPC_JOHNNYSDOMAIN_JOHNNNYSFOLDER_archive1.pst. Dim tArray: tArray=array(":",".","\",space(1)) strFixedName = strFile For iC = 0 To UBound(tArray) strFixedName= Replace(strFixedName,tArray(iC),"_") Next strDestinationPath = strDestinationFolder & "\" & strComputer & "_" & _ wshShell.ExpandEnvironmentStrings("%UserDomain%") & "_" & strFixedName & "_archive" & i & ".pst" fso.CopyFile strFile,strDestinationPath Next Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile" _ & " WHERE (Drive='X:') AND Extension='pst'") i = 0 For Each file in colFiles i = i + 1 strFile = File.Name 'CompName + UserName + archive (count).pst 'strOwner = FileOwner(strFile) 'StrOwner = Replace(strOwner,"\","_") 'strDestinationPath = strDestinationFolder & "\" & strComputer & "_" & strOwner & "_archive" & i & ".pst" 'or 'CompName + Domain + OriginalPathName + archive.pst (count +1), i.e. JOHNNYSPC_JOHNNYSDOMAIN_JOHNNNYSFOLDER_archive1.pst. Dim yArray: yArray=array(":",".","\",space(1)) strFixedName = strFile For iC = 0 To UBound(yArray) strFixedName= Replace(strFixedName,yArray(iC),"_") Next strDestinationPath = strDestinationFolder & "\" & strComputer & "_" & _ wshShell.ExpandEnvironmentStrings("%UserDomain%") & "_" & strFixedName & "_archive" & i & ".pst" fso.CopyFile strFile,strDestinationPath Next Function FileOwner(strFile) 'see http://msdn.microsoft.com/en-us/library/bb787870(VS.85).aspx Dim strFolder,strFileName Dim objShell dim objFolder dim objFolderItem Dim objInfo strFolder = fso.GetParentFolderName(strFile) strFileName = fso.GetFileName(strFile) set objShell = CreateObject("Shell.Application") Set objFolder = objShell.NameSpace(strFolder) Set objFolderItem = objFolder.ParseName(strFileName) FileOwner = objFolder.GetDetailsOf(objFolderItem, 8) Set objFolderItem = Nothing set objFolder = Nothing set objShell = Nothing End Function
All Replies
-
Wednesday, December 05, 2012 2:31 PM
I just got the username to work as part of the file name. Now I just need to trim down the file name.
I added
Set WshNetwork = WScript.CreateObject("WScript.Network") here:
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
dim wshShell
Set wshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim strDestinationFolder, strDestinationPathand then line 45 or so is now
strDestinationPath = strDestinationFolder & "\" & strComputer & "_" & _
wshShell.ExpandEnvironmentStrings("%UserDomain%") & "_" & WshNetwork.UserName & "_" & strFixedName & "_arc" & i & ".pst"If I remove the strFixedName it removed everything I don't want but also removes the drive letter which I *do* want ...
-
Wednesday, December 05, 2012 3:44 PMModerator
Hi,
The FileSystemObject object has methods to extract portions of a file specification. For example: GetParentFolderName, GetDriveName, GetExtensionName, GetBaseName, etc.
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Saturday, December 08, 2012 4:44 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 31, 2012 5:52 PM
-
Wednesday, December 05, 2012 4:06 PMCan't seem to make any of that replace the strFixedName portion though. It could just be me because I am really green and new at this scripting deal. I managed to add a function to kill Outlook to the script too so if someone has it open the pst file won't be locked.
-
Wednesday, December 05, 2012 4:50 PMModerator
Hi,
I suggest you start with a sample script that contains a filename in a string you want to "fix," and then experiment with the FileSystemObject methods to see how they work. For example:
Dim FileName FileName = "C:\Program Files (x86)\LibreOffice 3.6\program\swriter.exe" Dim FSO Set FSO = CreateObject("Scripting.FileSystemObject") ' Cutputs "C:" WScript.Echo "Drive: " & FSO.GetDriveName(FileName) ' Outputs "C:\Program Files (x86)\LibreOffice 3.6\program" WScript.Echo "Parent folder: " & FSO.GetParentFolderName(FileName) ' Outputs "swriter" WScript.Echo "Base name: " & FSO.GetBaseName(FileName) ' Outputs "exe" WScript.Echo "Extension: " & FSO.GetExtensionName(FileName)
Bill

