我是 PowerShell 菜鸟,正在寻找一种在目录中查找重复文件并将文件的文件路径写入文本文件或 csv 文件的方法。我当前的代码正在运行,但效率极低且速度缓慢。任何建议将不胜感激
#Declaring the Array to store file paths and names
$arr = (get-childitem "My Path" -recurse | where {$_.extension -like '*.*'})
#creating an array to hold already found duplicate elements in order to skip over them in the iteration
$arrDupNum = -1
#Declaring for loop to itterate the array
For ($i=0; $i -le $arr.Length - 1; $i++) {
$percent = $i / $arr.Length * 100
Write-Progress -Activity "ActivityString" -Status "StatusString" -PercentComplete $percent -CurrentOperation "CurrentOperationString"
$trigger = "f"
For ($j = $i + 1; $j -le $arr.Length - 1; $j++)
{
foreach ($num in $arrDupNum)
{
#if statement to skip over duplicates already found
if($num -eq $j -and $j -le $arr.Length - 2)
{
$j = $j + 1
}
}
if ($arr[$j].Name -eq $arr[$i].Name)
{
$trigger = "t"
Add-Content H:\Desktop\blank.txt ($arr[$j].FullName + "; " + $arr[$i].FullName)
Write-Host $arr[$i].Name
$arrDupNum += $j
}
}
#trigger used for formatting the text file in csv format
if ($trigger -eq "t")
{
Add-Content H:\Desktop\blank.txt (" " + "; " + " ")
}
}
使用哈希表按名称对文件进行分组:
$filesByName = @{}
foreach($file in $arr){
$filesByName[$file.Name] += @($file)
}
现在我们只需要找到包含多个文件的所有哈希表条目:
foreach($fileName in $filesByName.Keys){
if($filesByName[$fileName].Count -gt 1){
# Duplicates found!
$filesByName[$fileName] |Select -Expand FullName |Add-Content .\duplicates.txt
}
}
这样,当你有N
文件时,你最多会迭代它们N*2
几次,而不是N*N
多次:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句