user3702188
user3702188

Reputation: 69

search for file, when found in particular folder, move that folder to another location

I need help to be able to search on target folder for lastrefresh.xml file with last modified date older than 180 days. If it finds this file being older, whole folder where this XML file sits should be moved to archive path to another destination path.

$targetfolder = "\\something\*\*\*\*\*\files\files\
$archivefolder = "\\something\*\*\archive\

I'm using below script to have this working for folders, where it checks modified date of folder and this works.

$targetfolder = "\\something\*\*\*\*\*\files\files\
$archivefolder = "\\something\*\*\archive\

$LastWrite = (Get-Date).AddDays(-1)

$Files = Get-ChildItem $TargetFolder |
         Where-Object {$_.LastWriteTime -le $LastWrite} |
         Select-Object -ExpandProperty FullName |
         ForEach-Object {
           write-host "Archiving following folder" $_ $ArchiveFolder1
           Move-Item $_ $ArchiveFolder
         } 

But how could I have similar to above, where I only need to check for LastRefresh.xml files modified date in all targetfolder subfolders, and if it match my $lastwrite parameter for that XML file it would move whole folder with that file and all other files in that folder where it was found.

I only need to be able to check lastrefresh.xml files data modified. If it is older than 180 days, move whole folder where this XML file is present. This file is present in many folders.

Upvotes: 0

Views: 102

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

Try something like this:

Get-ChildItem $TargetFolder -File | ? {
  $_.Name -eq 'lastrefresh.xml' -and $_.LastWriteTime -le $LastWrite
} | % {
  Move-Item $_.Directory "$ArchiveFolder\"
}

Upvotes: 0

Related Questions