File 用于隐藏文件和系统文件的powershell测试

File 用于隐藏文件和系统文件的powershell测试,file,powershell,system,hidden,File,Powershell,System,Hidden,我想从我的驱动器中递归获取所有文件,我正在使用system.io.directory中的enumeratefiles,如下所示: [System.IO.Directory]::EnumerateFiles("J:\","*","AllDirectories")|out-file -Encoding ascii $outputfile foreach($line in select-string -Path $outputfile) { # Some of the $line is the n

我想从我的驱动器中递归获取所有文件,我正在使用system.io.directory中的enumeratefiles,如下所示:

[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:drive非常大,此函数运行速度很快,比等效的powershell cmdlet好得多

如何测试这些文件类型? 关于C++如何从枚举文件中退出这些文件类型,但也不适用于PosithBand,我不知道如何更改PosithSek的代码:

使用System.IO.FileInfo和一些枯燥的枚举魔法将得到您想要的

下面的示例将打印包含隐藏属性或系统属性的任何项目的完整路径

请注意,如果遇到您无权访问的文件或文件夹,则在停止执行时将抛出终止错误,您可以通过恢复到内置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)"
    }
}

谢谢你的建议

似乎在获取隐藏文件的文件属性时,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...
  #
}
[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...
  #
}