递归目录遍历中的非法字符

时间:2013-04-24 16:43:59

标签: powershell webdav directory subdirectory powershell-v3.0

我试图在已安装的WebDav驱动器上计算文件夹中的文件数量
如果路径或文件名中没有非法字符,而且本地磁盘上没有字符,则以下脚本可以正常工作 但是我在这个装载的驱动器(Linux)上有很多非法的Char

我尝试将$ErrorActionPreference = "SilentlyContinue"添加到脚本的开头,这样即使theres是一个错误它也会继续。

所以我添加Where-Object {Test-Path $_.FullName -isValid}来捕捉非法路径。

它仍违反了非法的Char ..

接下来的想法是从我的系统中获取非法字符并过滤包含它们的路径。

$illChars = [RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())
对我来说看起来很甜蜜,所以我可以很容易地在某处放置一个-match $illChars。 但仍然没有希望。

我认为在我甚至可以检查路径之前,错误会被抛出并打破Get-ChildItem $path上的管道(我对PowerShell Scripting来说真的很新,所以如果我是对或错的话,那就没有想法了)

我的大问题是:我怎样才能发现错误?

更新

如果$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 仍会抛出错误非法路径和文件并在Path处停止。 即使路径错误,Else(非法路径)也不会被调用。

#$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

1 个答案:

答案 0 :(得分: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填充到的位置:

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