将最新文件从文件夹复制到目标(Copy most recent file from folder to destination)

编程入门 行业动态 更新时间:2024-10-24 09:21:58
将最新文件从文件夹复制到目标(Copy most recent file from folder to destination)

我需要一些PowerShell的帮助。 我的经验非常有限,因为我是一个sql server dba。 我目前正在做的是从2000年到2008年迁移数据库。我想从源到目标自动化.bak副本,所以我不需要进入每个文件夹并复制然后粘贴。 所以让我给出一些图片。 源目录包含文件夹abcd 。 我希望脚本从每个文件夹中读取或理想地指定文件夹名称,并按日期获取最新的.Bak完整备份并复制到目标。 目标将具有相同的文件夹,因此文件夹a的备份复制到目标文件夹a将是很好的。 之后我想做同样的事情但是将我的搜索从完全备份搜索更改为差异。 任何帮助表示赞赏。

I need some assistance with powershell . My experience is very limited as I'm a sql server dba. What I am currently doing is migrating databases across from 2000 to 2008. I want to automate the .bak copy from source to destination so I don't need to go into each folder and copy and then paste. So let me give the picture. The source directory has folders a b c d. I want the script to read from each folder or ideally specify the folder names and get the most recent .Bak full backup by date and copy to a destination. The destination will have the same folders so folder a's backup copied to destination folder a will be great. Afterwards I want to do the same but change my search from full backup search to differential. Any help is appreciated.

最满意答案

这就是我要解决的问题。 文件夹路径和目标路径的变量是根文件夹,子文件夹的变量列出每个要搜索的文件夹。

Clear-Host $ChildFolders = @('A', 'B') for($i = 0; $i -lt $ChildFolders.Count; $i++){ $FolderPath = "D:\BackupSource\" + $ChildFolders[$i] $DestinationPath = "D:\BackupDestination\" + $ChildFolders[$i] gci -Path $FolderPath -File | Sort-Object -Property LastWriteTime - Descending | Select FullName -First 1 | %($_){ $_.FullName Copy-Item $_.FullName -Destination $DestinationPath } }

This is how I would address it. The vars for folderpath and destination path are the root folders and the var for childfolders list out each folder to search.

Clear-Host $ChildFolders = @('A', 'B') for($i = 0; $i -lt $ChildFolders.Count; $i++){ $FolderPath = "D:\BackupSource\" + $ChildFolders[$i] $DestinationPath = "D:\BackupDestination\" + $ChildFolders[$i] gci -Path $FolderPath -File | Sort-Object -Property LastWriteTime - Descending | Select FullName -First 1 | %($_){ $_.FullName Copy-Item $_.FullName -Destination $DestinationPath } }

更多推荐

本文发布于:2023-08-02 21:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   目标   文件   最新   folder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!