Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Script to copy newest version of particular files in a list

$
0
0

I'm trying to write a script that copies files between two remote servers. Files are all in one directory, however there are many files with "Similar" names going back 2 weeks. I only want the newest version of each file. There are 51 files I need, so I have to put them in a list.

For example, here's what the directory would look like:

  • SERVER_Active_Full_201401081244.safe  (1/8/14)  (I only want this one)
  • SERVER_Active_Full_201401071244.safe  (1/7/14)
  • SERVER_Active_Full_201401061244.safe  (1/6/14)
  • SERVER_Unicode_Full_201401081247.safe  (1/8/14)  (I only want this one)
  • SERVER_Unicode_Full_201401071247.safe  (1/7/14)
  • SERVER_Unicode_Full_201401061247.safe  (1/6/14) 

So out of those Active and Unicode, there are 51 varieties that I care about copying.  However there are others in the folder that I don't want to copy.  That's why I'm creating a full list of all 51 files.

I have a working script but it takes files from all dates, not just the newest version.  Basically, I'm looking for a way to incorporate this into the script below so that it only grabs the newest file of the files in the list:

If ($item.LastWriteTime -gt ((Get-Date).AddDays(-1)))

--------------------------------------------------------------------------

# source and destionation directory
$src_dir = "\\SERVER1\Test"
$dst_dir = "\\SERVER2\Test"

# list of files from source directory that I want to copy to destination folder
# unconditionally
$file_list = "SERVER_Active_Full*",
                 "SERVER_Unicode_Full*" ,
                 "SERVER_Test_Full*" ,
                   <etc, etc>

# Copy each file unconditionally (regardless of whether or not the file is there
foreach ($file in $file_list)
{
Copy-Item $src_dir$file $dst_dir
}



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>