Using the following code to select help select 1 file from a list of many dated files and then copy to share. Using the following code very kindly provided by fellow Stacker
$pastdays = -7
$pastdate = [datetime]::Now.AddDays($pastdays)
$files = Get-ChildItem "V:\Capacity Manager Size reports\"
$filename = ($files | Where-Object {$_.Name -like "Database_Server1_Userdb1_" + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
Copy-Item -Path $filename.FullName "\\Server50\report_Archive"
I need to expand this process to run for many files in the folder – each with slightly different names but following a common template to the file name
Original filenames examples:
Database_Server1_UserDB1_30_10_2012_00_20_51
Database_Server2_UserDB2_30_10_2012_01_20_51
Etc
Etc
But im not sure how to best do this in a foreach ?
I was thinking create a template file to load into a variable that holds all the examples of the files to examined before copying
So:
$Report_Template = Get-Content "C:\Powershell\FileTemplate_Name.txt";
FileTemplate_name.txt:
Database_Server1_tempdb_
Database_Server1_UserDB1_
Database_Server1_UserDB2_
Database_Server2_UserDB2_
Database_Server2_UserDB4_
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
$pastdays = -7
$pastdate = [datetime]::Now.AddDays($pastdays)
$files = Get-ChildItem "V:\Capacity Manager Size reports\"
foreach($template in $Report_Template)
$filename = ($files | Where-Object {$_.Name -like $template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
Copy-Item -Path $filename.FullName "\\Server50\CapacityManagerReports_Archive\"
Ive done the following which seems to work