I want to count duplicates of files with PowerShell. My files have a special separator ('#') and I can only compare the part before the separator.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 23.09.2016 09:44 0 AnotherDuplicateOffer_#1265473v1.DOCX -a---- 23.09.2016 09:44 0 AnotherDuplicateOffer_#89798798546v1.DOCX -a---- 23.09.2016 09:44 0 AnotherDuplicateOffer_#98769876v1.DOCX -a---- 23.09.2016 09:44 0 DuplicateOffer_#1254798v1.DOCX -a---- 23.09.2016 09:44 0 DuplicateOffer_#34987094587v1.DOCX -a---- 23.09.2016 09:44 0 DuplicateOffer_#4986598v1.DOCX -a---- 23.09.2016 09:44 0 DuplicateOffer_#567809v1.DOCX -a---- 23.09.2016 09:44 0 WordFilesAlthoug_#89798798546v1.DOCX
The part after the separator is a unique ID and at least I want to rename the files by removing this ID. So the new filename should be something like 'string (x).docx' and the 'x' should a counter for the duplicates.
I'm stuck by counting the duplicates:
foreach ($file in (Get-ChildItem -Path $path -Recurse | Where {!$_.PSIsContainer})) {
$file.Name
$file.Name.IndexOf("#")
$file.Name.Substring(0, ($file.Name.IndexOf("#")))
(dir *.* | group -Property Name | Where {($_.Name.Substring(0,($_.Name.IndexOf("#")))) -match ($_.Name.Substring(0,($_.Name.IndexOf("#"))))}).Count
}
I get the right index of '#' with $file.Name.IndexOf("#") and also the string of $file.Name.Substring(0,($file.Name.IndexOf("#"))) is right. But when I use the same in the pipe I get exceptions in Substring because of the second part - this must be greater than 0 and it appears to be 0 or less.
For better understanding: $_ is the same as $file - it is the actual pointer in the pipe.