PS 脚本复制新文件

PS script copy new files

编写 PS 脚本非常菜鸟 - 写了这个并一直在积极使用它,尽管仍然需要一些手动干预来尝试实现我的目标,我想完全自动化。

我会尽量解释清楚;

我正在尝试将“.bak”文件从源文件夹复制到特定目录,该源文件夹中每天都有文件被丢弃。问题是我创建脚本的方式,每次运行它都会创建一个新文件夹,其中包含一些与之前复制的文件相同的文件。

正在复制的文件在日期序列中都遵循相同的名称结构;

xxxx_2018_01_01_2131231.bak
xxxx_2018_01_02_2133212.bak  
xxxx_2018_01_03_2199531.bak

我如何编写脚本才能使其只复制较新的文件而不复制之前已复制的文件?

最好只创建一个新文件夹,然后更改文件名的某个部分。

这是脚本;

$basedir = "Path:\path"  
$today = (Get-Date).ToString('MM_dd_yy')  
$Filter = '*.bak'  
$location = New-Item -Path $basedir -Type Directory -Name $today  

Copy-Item -Path 'Path:\path' -Destination $location -Filter $Filter -Recurse

非常感谢任何指点! 预先感谢您的帮助!

我不确定是否有一种简单的方法来对此进行编码,但一般答案是使用 Get-ChildItem cmdlet。

"The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the -Recurse parameter to get items in all child containers and use the -Depth parameter to limit the number of levels to recurse."

通过使用 Get-ChildItem,您可以获得两个目录中的文件列表,然后比较它们是否具有相同的名称。然后根据您希望用来比较它们的标准构建一个 if() 参数。

这不是完整的答案,但它是一个很好的起点。

感谢大家的参与,不胜感激!

我已经切换到批处理文件路径并创建了以下内容来实现我的目标;

@echo off
setlocal

set _source="C:\users\user1\desktop\source"
set _dest="C:\users\user1\desktop\dest"

robocopy %_source% %_dest% *.bak /mir /XC /XN /XO

欢迎对此脚本提出任何意见!