Hi,
For files in subfolders "Get-ChildItem -Recurse -Name" returns "subfolder\filename", not only filenames. You may start with this.
$folder ="D:\test1"
$files = @(
"xyz.dat",
"two.txt",
"123.jpg"
)
$FolderToday = "D:\test2\today"
$FolderNew = "D:\test2\new"
$FolderOld = "D:\test2\old"
$TodaysFiles = @()
$NewFiles = @()
$OldFiles = @()
$folderFiles = Get-ChildItem -Path $folder -Recurse -File | Where-Object {$_.Name -in $files}
$folderFiles | ForEach-Object {
if($_.CreationTime.Date -eq [datetime]::Today){
$TodaysFiles += $_
}
elseif (([datetime]::Now - $_.CreationTime) -lt [timespan]::FromDays(1)){
$NewFiles += $_
}
else{
$OldFiles += $_
}
}
if($folderFiles.Count -eq 0){
#send email, saying no files in list are found in the folder.
}
else{
if($TodaysFiles.Count -ne 0){
Copy-Item -Path $TodaysFiles.FullName -Destination $FolderToday
#send email
}
if($NewFiles.Count -ne 0){
Copy-Item -Path $NewFiles.FullName -Destination $FolderNew
#send email
}
if($OldFiles.Count -ne 0){
Copy-Item -Path $OldFiles.FullName -Destination $FolderOld
#send email
}
}
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.