简体   繁体   English

将所有最新文件从文件夹/子复制到 powershell 中的同名文件夹/子

[英]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.

相关问题 使用Powershell将Folder中的所有文件替换为多个具有相同名称文件的子文件夹 - Replace all files in Folder with many sub folders with same name files with powershell Powershell和7zip-来自多个(子)文件夹的多个文件 - Powershell and 7zip - multiple files from multiple (sub)folders 使用Powershell处理多个子文件夹中的文件 - Coping files from multiple sub folders using powershell 使用 PowerShell 比较子文件夹和复制文件 - Comparing Sub-Folders and Copying Files with PowerShell Powershell 移动文件但不移动子文件夹 - Powershell move files but not sub-folders 此Powershell不会将项目计数分配给所有子目录或子目录或子目录 - This powershell doesnt give item count to all the sub or sub-sub or sub-sub-sub folders 重命名文件夹和子文件夹中的所有文件和文件夹 - Rename all files and folders in folder and sub folder PowerShell - 将特定文件从源子文件夹移动到具有相同子文件夹结构的目标 - PowerShell - Move specific files from source sub folders to destination with identical sub folder structure 使用 CMD 或 Powershell 从以特定名称开头的文件夹中复制文件 - Copy Files from Folders That Start with a Particular Name Using CMD or Powershell 使用 PowerShell 根据名称将文件列表复制到文件夹 - Copy list of files to folders depending on name with PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM