简体   繁体   English

powershell ls 错误某些文件夹/文件

[英]powershell ls errors certain folders / files

I'm looking at doing a recursive Get-ChildItem -r to get lastWriteTime, length and group for count by extension.我正在考虑执行递归 Get-ChildItem -r 以通过扩展获取 lastWriteTime、长度和组以进行计数。

I get a bunch of errors, eg, 'Get-ChildItem: Could not find item C:\Pics & Videos\Thumbs.db'.我收到一堆错误,例如“Get-ChildItem:找不到项目 C:\Pics & Videos\Thumbs.db”。

I was thinking some folders or filenames had special characters in the name of the folder or file.我在想某些文件夹或文件名在文件夹或文件的名称中有特殊字符。 I was able to encapsulate in quotes to correct some of the erroring files, but not all.我能够用引号封装来纠正一些错误文件,但不是全部。

[System.IO.File]::Exists("C:\Pics & Videos\Thumbs.db") gave me a True, but Get-ChildItem "C:\Pics & Videos\Thumbs.db" gave me the error. [System.IO.File]::Exists("C:\Pics & Videos\Thumbs.db") 给了我一个 True,但 Get-ChildItem "C:\Pics & Videos\Thumbs.db" 给了我错误。

I'm going to look at [System.IO.Fileinfo], but wonder if anyone can answer why I get these errors using Get-ChildItem aka ls?我将查看 [System.IO.Fileinfo],但想知道是否有人可以回答为什么我使用 Get-ChildItem aka ls 会出现这些错误?

Thanks谢谢

Thumbs.db is (typically) a hidden file. Thumbs.db (通常)是一个隐藏文件。 By default Get-ChildItem doesn't look for hidden files.默认情况下Get-ChildItem不查找隐藏文件。 Pass -Force ( -Hidden shows only hidden items):通过-Force-Hidden显示隐藏的项目):

PS> get-childitem .\Thumbs.db
Get-ChildItem: Could not find item C:\[...]\Thumbs.db.
PS> get-childitem .\Thumbs.db -Force

    Directory: C:\[...]

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a-h-          24/12/2016    11:17          13824 Thumbs.db

I may have found what I was looking for.我可能已经找到了我正在寻找的东西。 With $Path a full path to the starting folder I want to recursively get file info from.使用 $Path 是我想从中递归获取文件信息的起始文件夹的完整路径。

[IO.Directory]::EnumerateFileSystemEntries($path,"*.*","AllDirectories") | 
ForEach { [System.IO.FileInfo]"$_" }

Other suggestions are welcome that might be faster.欢迎其他可能更快的建议。 I'm looking at millions of files over 4500 folders.我正在查看超过 4500 个文件夹的数百万个文件。 get-childitem only was able to get 60% of the files with 40% being errors without values. get-childitem 只能获取 60% 的文件,其中 40% 是没有值的错误。 This is for one department and there are several.这是一个部门,有几个。

Tested: get-childItem vs EnumerateFiles vs Explorer vs TreeSize测试:get-childItem vs EnumerateFiles vs Explorer vs TreeSize

$path = "P:\Proxy Server Files\Dept1\sxs\"

First choice was slow.第一选择很慢。 I get errors;我得到错误; so I added the error count as a guess.所以我添加了错误计数作为猜测。

$error.clear()
(get-ChildItem $path -r -ErrorAction SilentlyContinue).count 
1333
$error.count
256

Second choice was much faster but gave less numbers.第二个选择要快得多,但给出的数字更少。

$error.clear()
([IO.Directory]::EnumerateFileSystemEntries($path,"*.*","AllDirectories")).count
1229
$error.count
0

Trying to only look at files recursively again I get errors;尝试再次递归地查看文件时出现错误; so I added the error count as a guess.所以我添加了错误计数作为猜测。

$error.clear()
(get-ChildItem $path -r -file).count
558
$error.count
256

Looking at just files I get a much lower number that expected.只看文件,我得到的数字比预期的要低得多。

([IO.Directory]::EnumerateFileSystemEntries($path,"*.*","AllDirectories") | ForEach { [System.IO.FileInfo]"$_" }| Where Mode -NotMatch "d").count
108

Tried another method but same result.尝试了另一种方法,但结果相同。

([IO.Directory]::EnumerateFiles($path,"*.*","AllDirectories")| ForEach { [System.IO.FileInfo]"$_" }| Where Mode -NotMatch "d").count
108

From Windows Eplorer I see 37 files and 80 folders.从 Windows Eplorer 我看到 37 个文件和 80 个文件夹。

TreeSize.exe shows 1175 files on 775 folders. TreeSize.exe 显示 775 个文件夹中的 1175 个文件。

I'm not sure what count to believe.我不确定该相信什么。 Admin rights used to get all counts.用于获取所有计数的管理员权限。

Any ideas why so many different results?任何想法为什么会有这么多不同的结果?

This was answered in question 9508375.这在问题 9508375 中得到了回答。

get-childitem -literalpath

This handles the problem with special characters in the name.这可以处理名称中特殊字符的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM