So I have 150 folders with 100's of files named like this.. M10001662_3269506_DVLA_Original_Complete_archived_201208_2226.pdf M10001662_3269506_DVLA_Original_Complete_archived_201209_2203.pdf M10001662_3269506_DVLA_Original_Complete_archived_201210_2231.pdf M10001662_3269506_DVLA_Original_Complete_archived_201211_2202.pdf
EDIT There are about 10 different file names, all with "_archived_YYYYMMDD_HHMM.pdf" added to the end. I just need one (they are all identical, so anyone will do) of each file without the "_archived_YYYYMMDD_HHMM.pdf" on the end. NB: Theo has asked for desired output - From many files like M10001662_3269506_DVLA_Original_Complete_archived_201208_2226.pdf To One file like M10001662_3269506_DVLA_Original_Complete.new
Was looking to rename the files I want to keep with *.new Then delete all *.pdf files (Just as I know how to do that) and finally rename the .new files back to *.pdf Leaving me with only one of each file.
Hope this has made it clearer, END EDIT
I have the following code, which gives me the Filenames of the ones I want to keep, but I cannot actually action the rename bit..
# Remove duplicate files - based on name/string search
# set the rootfolder to search
$dir = 'O:\UKCH-DATA\Contact Centre\04 - Main\14 - DVLA\_case_files_archive\M10001662'
# loop through the directories directly under $dir
Get-ChildItem -Path $dir -Filter "*.pdf" |
Group-Object -property {$_.BaseName.split('archived', 2)[0]} |
ForEach-Object { $_.Group | Select-Object -First 1}
#Write-Output $Filename.VersionInfo |
#Rename-Item $Filename.VersionInfo -NewName -replace ("\.pdf", ".new")
Having trouble with the first part of renaming, so please help