INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Copy most recent files from a list of directories

Copy most recent files from a list of directories

(OP)
I want to give PowerShell an explicit list of file paths to several directories and have it copy the most recent file found in each directory to a common location.

e.g., given the list

Z:\RTPSQL\DataFileBackups\RTPOne
Z:\Dynamics-GP
Z:\TimeCentre\tc2kdb
Z:\FoodTrak
Z:\FleetMax

I want to get the most recent file out of each of those directories and copy it to another directory named

C:\ForArchive

I have scripts to copy the entire contents of a directory, or to copy an explicit file name from a directory, but not to get the most recent file regardless of its name.

Suggestions?
 

-
Richard Ray
Jackson Hole Mountain Resort

RE: Copy most recent files from a list of directories

i don't know the syntax, but the logic would look like this:
for each directory
1. get a list of all files in the directory
2. order files by create date in descending order
3. take the first file
4. copy the file

in c# this is simple enough

CODE

new [] {
@"Z:\RTPSQL\DataFileBackups\RTPOne",
@"Z:\Dynamics-GP",
@"Z:\TimeCentre\tc2kdb",
@"Z:\FoodTrak",
@"Z:\FleetMax"
}
.Select(directory => new DirectoryInfo(directory))
.Where(directory => directory.Exists)
.Select(directory => directory
                      .GetFiles()
                      .OrderByDescending(file => file.CreationTime)
                      .FirstOrDefault())
.Where(file => file != null)
.ToList()
.ForEach(file => File.CopyTo(@"C:\ForArchive\")
with PS i'm sure you could somehow pipe for a clean script. something like

CODE

@(
"Z:\RTPSQL\DataFileBackups\RTPOne",
"Z:\Dynamics-GP",
"Z:\TimeCentre\tc2kdb",
"Z:\FoodTrak",
"Z:\FleetMax"
)
select-object {get-childitem $_ | sort-object $_.CreationTime -desc | select-object -first 1} | where $_ != null | copy-object $_ "c:\ForArchive\"

Jason Meckley
Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close