2013-04-24 49 views
0

即時嘗試計算在已安裝的WebDav驅動器上的文件夾中的文件數量遞歸
如果在路徑或文件名中沒有非法字符,並且像本地磁盤上的符號一樣,以下腳本可以正常工作。
但是我有相當多的是安裝驅動(Linux)的非法字符在遞歸目錄遍歷

非法字符的我嘗試添加$ErrorActionPreference = "SilentlyContinue"到腳本的開始,這樣即使是孤單一個錯誤繼續。

所以我加了Where-Object {Test-Path $_.FullName -isValid}來抓非法Path。

它仍然打破對非法字符的..

下一頁愛迪爾是從我的系統的非法字符的和篩選所包含它們的路徑的。

$illChars = [RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars()) 

看起來很甜,所以我可以很容易地把-match $illChars放在某個地方。 但仍然沒有希望。

我認爲錯誤得到拋出,打破對Get-ChildItem $path水管之前,我甚至可以檢查路徑(IM真正的新的PowerShell腳本所以沒有IDEAR如果即時通訊正確或錯誤)

我的大問題是:我怎樣才能捕捉到錯誤?

更新:

誤差會拋出如存在是illega文件名中$Files = @(Get-ChildItem -File $_.FullName)

確切的錯誤信息是

Get-ChildItem : Illegales Zeichen im Pfad. 
In ***** count-files.ps1:12 Zeichen:15 
+    $Files = @(Get-ChildItem -File $_) 
+      ~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (L:\flash\fs\trailer:String) [Get-ChildItem], ArgumentException 
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand 

Get-ChildItem : Illegales Zeichen im Pfad. 
In ***** \count-files.ps1:6 Zeichen:1 
+ Get-ChildItem $path -recurse -exclude ".DAV" -Directory | 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (L:\flash\fs\trailer:String) [Get-ChildItem], ArgumentException 
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand 

更新2

仍然相同錯誤,但現在腳本繼續。好極了。
是否可以完全消除錯誤?

更新3

錯誤 「路徑格式」
和腳本再次停止
有關使用diruse.exe思考...

Get-ChildItem : Das angegebene Pfadformat wird nicht unterstützt. 
In D:\workspace\videoRename\count-files.ps1:13 Zeichen:15 
+    $Files = @(Get-ChildItem -File $_.FullName | where { $_.Name -notmatch $patte ... 
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [Get-ChildItem], NotSupportedException 
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.GetChildItemCommand 

Get-ChildItem : Das angegebene Pfadformat wird nicht unterstützt. 
In D:\workspace\videoRename\count-files.ps1:7 Zeichen:1 
+ Get-ChildItem $path -recurse -exclude ".DAV" -Directory | where { $_.Name -notma ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [Get-ChildItem], NotSupportedException 
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.GetChildItemCommand 

好吧,有什麼像GetInvalidPathChars
讓我們檢查路徑...

更新4.1 仍然拋出錯誤非法路徑和文件並停止在路徑。 其他(非法路徑)不會被調用,即使在路徑錯誤。

#$ErrorActionPreference = "SilentlyContinue" 
$path="L:\" 
$illName = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())) 
$illPath = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidPathChars())) 

Get-ChildItem $path -recurse -exclude ".DAV" -Directory | where { $_.Name -notmatch $illName } | where { $_.FullName -notmatch $illPath } | 
    foreach-object { 
     write-host $_ 
     if((Test-Path $_ -isValid) -and (Test-Path $_.FullName -isValid) -and ($_.FullName -notmatch $illPath)){ 
      write-host $_ 
      $FolderDepth = ($_.FullName.Split("\\") | measure-object).Count - 1 
      $Files = @(Get-ChildItem -File $_.FullName | where {Test-Path $_ -isValid} | where { $_.Name -notmatch $illName }) 
      $Size = 0 
      $Files | % {$Size = $Size + $_.Length} 
      New-Object PsObject -Property @{ 
       'Directory' = $_.FullName 
       'Files' = $Files.Count 
       'Depth' = $FolderDepth 
       'Size' = $Size 
      } 
     }else { 
      write-host "Error in Folder:" $_.FullName 
      New-Object PsObject -Property @{ 
       'Directory' = $_.FullName 
       'Files' = -1 
       'Depth' = -1 
       'Size' = -1 
      } 
     } 
    } | 
export-csv -notypeinformation -Delimiter ";" -encoding default -path test.csv 

回答

0

我不能從這裏輕鬆測試這個,但是像下面這樣的東西?

Get-ChildItem $path -recurse -Directory | 
    foreach-object { 
    if((Test-Path $_ -isValid) -and ($_ -notMatch ".DAV")) { 

按照你的更新,讓這個去...在你的腳本插件頂部:

$pattern = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())) 

然後修改,你填充$文件:

$Files = @(Get-ChildItem -File $_.FullName -ErrorAction SilentlyContinue | 
    ? { $_.Name -notmatch $pattern }) 
+0

THX爲答案,但它在同一個位置上休息。我再次運行錯誤輸出,所以我可以更具體。 – 2013-04-25 12:29:18

+0

我編輯了我的問題。也許你有antoher暗示? – 2013-04-25 13:10:23