将最近的文件从文件夹复制到目的地 [英] Copy most recent file from folder to destination

查看:49
本文介绍了将最近的文件从文件夹复制到目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关 powershell 的帮助.我的经验非常有限,因为我是 sql server dba.我目前正在做的是将数据库从 2000 年迁移到 2008 年.我想自动化从源到目标的 .bak 副本,因此我不需要进入每个文件夹并复制然后粘贴.所以让我给图片.源目录包含文件夹a b c d.我希望脚本从每个文件夹中读取或理想地指定文件夹名称并获取最新的.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.

推荐答案

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

This is how I would address it. The variables for folderpath and destination path are the root folders and the variable 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 
    }

}

这篇关于将最近的文件从文件夹复制到目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆