Link to home
Start Free TrialLog in
Avatar of Andrew Hamilton
Andrew HamiltonFlag for United States of America

asked on

Need to consolidate files from a folder keeping only the most recent version of a file name by date created

I have a folder that is used to hold several versions of files.   The main folder contains many sub-folders all named by date.   A particular file can occur in any date folder if it has been revised as it retains the same name.     What i need to do is copy out all the files from this folder structure and get only the most recent version of any file by name ignoring by the date of the file as listed in the OS.
Avatar of oBdA
oBdA

So you want the latest files from all folders, based on the parent folder's "date name"?
Then we need to know the exact naming format of these subfolders that are named by date.
Avatar of Andrew Hamilton

ASKER

Based on the date of the file within the folder,  not the name of the parent folder containing the file.
So just unique file names with the latest LastWriteTime file attribute?
In test mode, will only list the files it would normally copy. Remove the -WhatIf in the last line to run it for real.
$source = 'C:\Temp\Source'
$destination = 'C:\Temp\Target'
Get-ChildItem -Path $Source -Recurse -File |
	Select-Object -Property @{n='Path'; e={$_.FullName}}, Name, LastWriteTime |
	Group-Object -Property Name |
	ForEach-Object {$_.Group | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1} |
	Copy-Item -Destination $destination -WhatIf

Open in new window

This appears to do what it's supposed to except it's not copying the files into the destination folder.
See the line with the bold part in the post with the script ...
I did remove the -WhatIf
It works just fine here. Does it list the files if you add -Verbose instead of -WhatIf in the last line?
I ended up using access lists and attaching them on the neighbor definition
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.