0

I am trying to copy the latest file from every folder/sub-folder into a same file structure on a different drive.

Latest file from source copied to the same name corresponding destination.

The destination folder hierarchy already exists & cannot be copied over or recreated. This & other versions are not behaving. Can anyone help?

$sourceDir = 'test F Drive\Shares\SSRSFileExtract\'

$destDir = 'test X Drive\SSRSFileExtract\'

$date = Get-Date

$list = Get-ChildItem -Path $sourceDir | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
    
foreach ($item in $list)
{   
Copy-Item -Verbose -LiteralPath $item.FullName -Destination $destDir -Force |
Get-Acl -Path $item.FullName | Set-Acl -Path $destDir\$(Split-Path -Path $item.FullName -Leaf)
}

Get-ChildItem –Path $destDir -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-5))} | Remove-Item -Verbose -Recurse -Force
Riz Kler
  • 11
  • 1
  • Wow, this is a tricky one. First you will need to recursive look into every folder below `$sourceDir`. This can be done by adding the `-Recurse` parameter when declaring the `$list` variable. Not sure how the pipe will behave as I'm on my phone. If the pipe works ok you might be able to loop it, but I don't want to incur in any wild assumptions. – alexzelaya Jul 15 '21 at 23:53
  • Thanks for your reply but it still keeps copying the folder into the destination which I don't want as the destination must only be updated with the latest files in each sub folder. Source & Destination have the same sub-folders. – Riz Kler Jul 16 '21 at 13:27

2 Answers2

0

I found a solution for this which copies/moves all the files from all sub folders in to all corresponding sub folders:

Powershell: Move all files from folders and subfolders into single folder

Riz Kler
  • 11
  • 1
0

The way your code retrieves the list of files will only return one single object because of Select-Object -First 1. Also, because you don't specify the -File switch, Get-ChildItem will also return DirectoryInfo objects, not just FileInfo objects..

What you could do is get an array of FileInfo objects recursively from the source folder and group them by the DirectoryName property Then loop over these groups of files and from each of these groups, select the most recent file and copy that over to the destination folder.

Try:

$sourceDir = 'F:\Shares\SSRSFileExtract'
$destDir   = 'X:\SSRSFileExtract'

Get-ChildItem -Path $sourceDir -File -Recurse | Group-Object DirectoryName | ForEach-Object {
    # the $_ automatic variable represents one group at a time inside the loop
    $newestFile = $_.Group | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
    # construct the target sub directory
    # you could also use $_.Name (the name of this group) instead of $newestFile.DirectoryName here, because
    # we grouped on that DirectoryName file property.
    $targetDir = Join-Path -Path $destDir -ChildPath $newestFile.DirectoryName.Substring($sourceDir.Length)

    # if you're not sure the targetpath exists, uncomment the next line to have it created first
    # if (!(Test-Path -Path $targetDir -PathType Container)) { $null = New-Item -Path $target -ItemType Directory }

    # copy the file
    $newestFile | Copy-Item -Destination $targetDir -Force -Verbose
    # copy the file's ACL
    $targetFile = Join-Path -Path $targetDir -ChildPath $newestFile.Name
    $newestFile | Get-Acl | Set-Acl -Path $targetFile
}

Apparently you would also like to clean up older files in the destination folder

Get-ChildItem –Path $destDir -File -Recurse | 
    Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-5).Date} | 
    Remove-Item -Verbose -Recurse -Force

Be aware that the final code to remove older files could potentially remove all files from a subfolder if all happen to be older than 5 days..

Theo
  • 57,719
  • 8
  • 24
  • 41