Announcement

Collapse
No announcement yet.

Search all pst file in root and exclude folder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search all pst file in root and exclude folder

    Hello,
    My script scan or search to f:\ root any file with .pst extension and copy to another folder, but i'd like this script exclude folder F:\Documents and Settings\rgaspar\Local Settings\Application Data\Microsoft\Outlook, can i help me?

    Code:
    Option Explicit
    Dim oFSO
    Dim strUserName
    Dim oShell
    Dim oEnv
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oShell = CreateObject("Wscript.Shell")
    Set oEnv = oShell.Environment("SYSTEM")
    strUserName = oShell.ExpandEnvironmentStrings("%username%")
     
    SearchPST (oFSO.GetFolder("F:\"))
     
    ExcludeFolder (oFSO.GetFolder("F:\Documents and Settings\rgaspar\Local Settings\Application Data\Microsoft\Outlook"))
    '*************************************************
    Sub SearchPST(objCurrentFolder)
       Dim strTemp
       Dim strOutput
       Dim objNewFolder
       Dim objFile
       On Error Resume Next
     
       For Each objFile In objCurrentFolder.Files
        strTemp = Right(objFile.Name, 4)
         If UCase(strTemp) = ".PST" Then
       wscript.echo CStr(objFile.Path)
       'oFSO.CopyFile CStr(objFile.Path), "D:\Dados\" & StrUsername & ".$$$" & "\OldPST\" & Date & " " & CStr(objFile.Name)
      End If
     Next
     
     For Each objNewFolder In objCurrentFolder.subFolders
      SearchPST (objNewFolder)
     Next
     
     
     On Error Goto 0
     
    End Sub
    Thanks

  • #2
    Re: Search all pst file in root and exclude folder

    Try using robocopy - that has the option to exclude certain paths.
    Gareth Howells

    BSc (Hons), MBCS, MCP, MCDST, ICCE

    Any advice is given in good faith and without warranty.

    Please give reputation points if somebody has helped you.

    "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

    "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

    Comment


    • #3
      Re: Search all pst file in root and exclude folder

      Basically you just have to add the line:
      If NOT LCase(objFolder.path) = ExcludeDir Then ......
      just before the line: For Each File In objFolder.Files

      where variable ExcludeDir = LCase("F:\Documents and Settings\rgaspar\Local Settings\Application Data\Microsoft\Outlook")


      Or, If you like to exclude the "\Local Settings\.\.\." path of the user currently logged on and running the script then:
      Code:
      Dim oFSO
      Dim strAPPDATA, ExcludeDir
      
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      
      strAPPDATA = UserShellFolder("Local AppData") 'current user
      ExcludeDir = LCase(strAPPDATA & "\Microsoft\Outlook")
      
      call SearchPST( "F:\" )
      
      wsh.echo "done"
      
      Sub SearchPST(Folder)
        Dim objFolder, Subfolder
        Dim File, sFileExt
        Set objFolder = oFSO.GetFolder(Folder)
        rem If NOT LCase(objFolder.path) = ExcludeDir Then
          On Error Resume Next
          For Each File In objFolder.Files
            If err.number = 0 Then
              sFileExt = oFSO.GetExtensionName(File.name)
              If LCase(sFileExt) = "pst" Then
                 wscript.echo File.Path
              End If
            Else err.clear
            End If
          Next
        rem End If
        File = empty : sFileExt = empty
        '---recursive function---
        For Each Subfolder in objFolder.SubFolders
          err.clear
          If NOT LCase(Subfolder.Path) = ExcludeDir Then
            Call SearchPST( Subfolder )
          Else wsh.echo "excluded: ", Subfolder.Path
          End If
        Next
      End Sub
      
      Function UserShellFolder(ShFldr)
        Dim strRegKey, strShellFolder
        With WScript.CreateObject("WScript.Shell")
          strRegKey = "HKCU\Software\Microsoft\Windows" _
             & "\CurrentVersion\Explorer\Shell Folders\"
          strShellFolder = strRegKey & ShFldr 
          UserShellFolder = .RegRead(strShellFolder)
          UserShellFolder = .ExpandEnvironmentStrings(UserShellFolder)
        End With
      End Function
      If you like to exclude the ".\.\.\Local Settings\.\.\." path in all the stored user profiles on F: then:
      Code:
      Dim oFSO
      Dim ExcludeDir
      
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      
      Dim RegEx : Set RegEx = New RegExp
      ExcludeDir = "F:\Documents and Settings\*\local settings\application data\Microsoft\Outlook"
      ExcludeDir = Replace(Replace(ExcludeDir,"\","\\"),"*","([^\\]*)")
      RegEx.Pattern = "^"  & ExcludeDir & "$"
      
      RegEx.IgnoreCase = True
      RegEx.Global = True
      
      call SearchPST( "F:\" )
      
      wsh.echo "done"
      
      Sub SearchPST(Folder)
        Dim objFolder, Subfolder
        Dim File, sFileExt
        Set objFolder = oFSO.GetFolder(Folder)
        On Error Resume Next
        For Each File In objFolder.Files
          If err.number = 0 Then
            sFileExt = oFSO.GetExtensionName(File.name)
            If LCase(sFileExt) = "pst" Then
               wscript.echo File.Path
            End If
          Else err.clear
          End If
        Next
        File = empty : sFileExt = empty
        '---recursive function---
        For Each Subfolder in objFolder.SubFolders
          err.clear
          If (RegEx.Test(Subfolder.Path) = False) Then
            Call SearchPST( Subfolder )
          Else wsh.echo "excluded: ", Subfolder.Path
          End If
        Next
      End Sub
      \Rems
      Last edited by Rems; 18th November 2008, 22:01. Reason: added colors

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Search all pst file in root and exclude folder

        Thanks Rems.

        I testing you code and run perfectly.

        Comment

        Working...
        X