Home > Mobile >  powershell ls errors certain folders / files
powershell ls errors certain folders / files

Time:07-12

I'm looking at doing a recursive Get-ChildItem -r to get lastWriteTime, length and group for count by extension.

I get a bunch of errors, e.g., 'Get-ChildItem : Could not find item 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.

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?

Thanks

CodePudding user response:

Thumbs.db is (typically) a hidden file. By default Get-ChildItem doesn't look for hidden files. Pass -Hidden:

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

    Directory: C:\[...]

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

CodePudding user response:

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.

[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. get-childitem only was able to get 60% of the files with 40% being errors without values. This is for one department and there are several.

CodePudding user response:

Tested: 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.

TreeSize.exe shows 1175 files on 775 folders.

I'm not sure what count to believe. Admin rights used to get all counts.

Any ideas why so many different results?

  • Related