Powershell - IO.Directory - Find file types in all subdirectories

前端 未结 3 1292
天命终不由人
天命终不由人 2021-01-13 15:20

I ran across this code in another post that almost does what I need, but can\'t figure out how to modify it to look for specific file types, i.e. *.bak, .txt, etc. I\'m

3条回答
  •  无人共我
    2021-01-13 16:11

    This worked after creating a config file to use the .NET 4.0 Framework:

    try{
        $path = "\\$Server\$($drive.replace(':','$'))"
        foreach ($filepath in [System.IO.Directory]::EnumerateFiles($path,"*.bak","AllDirectories"))
           {
              $file = New-Object System.IO.FileInfo($filepath)
              write-host $file
              # insert file into database table
           }
      }
    catch [UnauthorizedAccessException]
      {
         $exception = $_.Exception.Message #I don't need to write/insert exception
      }
    

提交回复
热议问题