2017-10-11 135 views
0


我想从我的硬盘递归得到所有的文件和我使用enumeratefiles从system.io.directory如下PowerShell的测试:隐藏文件和系统文件

[System.IO.Directory]::EnumerateFiles("J:\","*","AllDirectories")|out-file -Encoding ascii $outputfile 
foreach($line in select-string -Path $outputfile) { 
    # Some of the $line is the name of a hidden or system file 
} 

但是这工作正常,许多行包含隐藏或系统文件。
我已经使用ennumeratefiles作为j:驱动器非常大,并且此功能的运行速度快于等效的powershell cmdlet。

我该如何测试这些文件类型?

有关于如何exlude从enumeratefiles对于C这些文件类型的东西+ +,但也不是为PowerShell和我不知道如何更改PowerShell中的代码:
c++ file hidden or system

回答

0

使用System.IO.FileInfo和一点点-bor荷兰国际集团枚举魔术会让你想要你想要的。

下面的示例将打印包含属性HiddenSystem的任何项目的完整路径。

$hidden_or_system = [System.IO.FileAttributes]::Hidden -bor [System.IO.FileAttributes]::System 

[System.IO.Directory]::EnumerateFiles("J:\","*","AllDirectories") | ForEach-Object { 
    if ([System.IO.FileInfo]::new($_).Attributes -band $hidden_or_system) { 
     "A hidden or system item: $_" 
    } 
} 

被警告说,如果你遇到一个文件或文件夹,您没有权限访问终止错误将被抛出停止执行,您可以解决此通过恢复到内置的cmdlet,因为他们将会抛出无法终止的错误并继续。

$hidden_or_system = [System.IO.FileAttributes]::Hidden -bor [System.IO.FileAttributes]::System 

Get-ChildItem -Path 'J:' -Recurse -Force | ForEach-Object { 
    if ($_.Attributes -band $hidden_or_system) { 
     "A hidden or system item: $($_.FullName)" 
    } 
} 
0

感谢您的建议。

看起来,当您获取“隐藏”文件的文件属性有时powershell抛出一个非终止错误。

[System.IO.Directory]::EnumerateFiles("J:\","*","AllDirectories")|out-file -Encoding ascii $outputfile 
foreach($line in select-string -Path $outputfile) { 
    # Some of the $line is the name of a hidden or system file 
    if ((Get-ItemProperty $line -ErrorAction SilentlyContinue).attributes -band [io.fileattributes]::Hidden) {continue} 
    if (-not $?) {continue} 
    # Is the file a system file? 
    if ((Get-ItemProperty $line -ErrorAction SilentlyContinue).attributes -band [io.fileattributes]::System) {continue} 
    if (-not $?) {continue} 
    # 
    # Do the work here... 
    # 
} 

我使用建议固定问题