简体   繁体   中英

Powershell -Rename files from "Group-Object" output

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

You can do it way easier:

Get-ChildItem -Path $dir -Filter "*.pdf" |
  Group-Object -property { $_.BaseName.split('archived', 2)[0] } | foreach {
    # rename the file to keep (the 1st file from the group)
    $_.Group | select -First 1 | Rename-Item -NewName ($_.Name + ".pdf")
    # delete the others:
    $_.Group | select -Skip 1 | Remove-Item
}

For the renaming part you can use a delay-bind script block :

# ... represents your command from the question.
... | Rename-Item -NewName { $_.Name -replace '_archived.+', '.new' } -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Note: I'm assuming you want to remove the _archived* part from the filename too, which is what the above -replace operation does; if you simply want to change the filename extension from .pdf to .new , use { $_.BaseName + '.new' } .


Since the above changed the filename extension of the files you want to keep, you can then delete the .pdf files left behind:

Remove-Item -Path $dir\*.pdf -WhatIf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM