'Resolve-Path: cannot find path because it does not exist. error in powershell

Firstly: what My code does it counts the number of duplicate files in a particular path entered by the user. It does this by first calculating which files have the same length. and from the files that have the same length, it checks which files also have the same hash. And then in the end I am making an object which contains the information that I need to print in a table.

Problem: In my code,

  1. I am getting an error which I am pretty sure is coming from the Get-FileHash commandlet. when I ctrl + click on the link that is given in the "At" section of the error, It takes me to the Get-FileHash commandlet utility in powershell C: >WIndows > system32 > WindowsPowershell > v1.0 > Modules > Microsoft.Powershell.Utility > Microsoft.Powershell.Utility.psm1 > Get-FileHash{}

  2. the error only comes up for some .msg and .pdf files

  3. the code works perfectly fine on a path with lesser files.

Here is the Error:

Resolve-Path : Cannot find path '<path of File>' because it does not exist.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Utility\Micro
+ ...     $pathsToProcess += Resolve-Path -LiteralPath $LiteralPath | Forea ...
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (<nameOfFile>:String)
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathComm

And here is the code:

#taking the input of the user!
$pathInputByUser = Read-Host ("Please Enter the Path where you want to check for duplicate files")



#Accessing the path entered by the user
cd $pathInputByUser
write-host ("Loading: Please Wait! This can take a upto 2.5 hrs") -ForegroundColor Green
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew();


$totalNumOfFiles = 0;

#the variable "filesGroupedAccordingToLength" contains all the files grouped according to their size. files with same size , are in the same group
$filesGroupedAccordingToLength = Get-ChildItem  -Recurse -File `
| Group-Object -Property Length

#below  calculating the number of total Files that we have found till now
$totalNumOfFiles = $filesGroupedAccordingToLength | Measure-Object -Property Count -Sum | % { $_.sum };


#the below variable "filesWhichHaveSameLengths" contains group of files that have the same size as some other file
$filesWhichHaveSameLengths = $filesGroupedAccordingToLength | ? { $_.Count -gt 1 } `
| % { $_.Group } 




#the below variable "hash"  contains group of files that have the same hash as some other file
$groupedFilesWithSameHash = $filesWhichHaveSameLengths | Get-FileHash `
| Group-Object -Property Hash `
| ? { $_.Count -gt 1 }


$numOfDuplicates= 0;
$calculatingCountDuplicates= $groupedFilesWithSameHash| Select-Object -ExpandProperty Count| %{ $numOfDuplicates= $numOfDuplicates + ($_ - 1) };
"The number of duplicate files are: $numOfDuplicates"

#the below variables contains hash of duplicate files
$hashOfDuplicateFiles= $groupedFilesWithSameHash | % { $_.Group }


#making objects to be able to display result as a table
$res= @();
$hashValue=0;
 foreach ($tuple in $hashOfDuplicateFiles) {
    foreach ($var in $filesWhichHaveSameLengths) {
        if ($var.FullName -eq $tuple.Path) {
            $obj=  new-object psobject -Property @{
                LastWriteTime = $var.LastWriteTime
                Name          = $var.Name
                length        = $var.length
                FullName      = $var.FullName
                hash          = $tuple.Hash
                RelativePath  = $var.PSPath.Replace("Microsoft.PowerShell.Core\FileSystem::F:\", "")
            }
            if($tuple.Hash -ne $hashValue){
                $obj|Add-Member -NotePropertyName U/D -NotePropertyValue "Unikat"
                $res= $res+$obj;
                $hashValue= $tuple.Hash;
            }
            else{
                $obj|Add-Member -NotePropertyName U/D -NotePropertyValue "Duplikat"
                $res= $res+$obj;
            }
       
        }
    }
}




$res | Format-Table -AutoSize -Property Name, U/D,  LastWriteTime, hash, length, RelativePath |Out-File -FilePath C:\Users\Public\Documents\Ebrahim_iris\DuplicateFinderTool\testLog.txt 

"the number of total files is : $totalNumOfFiles"
"The number of duplicate files are: $numOfDuplicates"
"The number of unique files are: $($totalNumOfFiles-$numOfDuplicates)"

$Stopwatch.Stop();
"The script ran for: $($Stopwatch.Elapsed.TotalMinutes) minutes"

Any advice to remove this error will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source