[英]Copy all latest files from folders/sub to the same name folders/sub in powershell
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
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 Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中
The way your code retrieves the list of files will only return one single object because of Select-Object -First 1
.由于
Select-Object -First 1
您的代码检索文件列表的方式将仅返回一个对象。 Also, because you don't specify the -File
switch, Get-ChildItem will also return DirectoryInfo objects, not just FileInfo objects..此外,因为您没有指定
-File
开关,Get-ChildItem 还将返回 DirectoryInfo 对象,而不仅仅是 FileInfo 对象。
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.您可以做的是从源文件夹中递归获取一组 FileInfo 对象,并通过 DirectoryName 属性对它们进行分组,然后循环遍历这些文件组并从这些组中的每一个中选择最新的文件并将其复制到目标文件夹.
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..请注意,如果所有文件碰巧超过 5 天,则删除旧文件的最终代码可能会从子文件夹中删除所有文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.