使用PowerShell匹配文件名和副本

mea*_*ies 6 powershell copy file

我正在努力让这个简单的PowerShell脚本工作.我用谷歌搜索,但我找不到答案.

如您所见,我正在指定源位置和目标位置.

$ filedate变量正在执行dateadd以获取昨天的日期,因为文件名包含日期.

$ filter变量包含我正在搜索的字符串,包括日期部分.

Get-ChildItem命令可以自行运行,但是当我应用过滤器时,它什么都不返回.为了让这个工作,我错过了什么?

$source = "C:\MSSQL.1\Backup\"
$destination = "D:\MSSQL.2\Backup\"
$filedate = (get-date).AddDays(-1).tostring('yyyyMMdd')
$filter = "FULL_(local)_Product_" + $filedate + "*"
Get-ChildItem -Path $source -filter $filter | Copy-Item -Destination $destination
Run Code Online (Sandbox Code Playgroud)

Enr*_*lio 14

尝试使用Where-Object cmdlet-match运算符筛选文件列表:

$source = "C:\MSSQL.1\Backup\"
$destination = "D:\MSSQL.2\Backup\"
$filedate = (Get-Date).AddDays(-1).ToString("yyyyMMdd")
$filter = "FULL_(local)_Product_$filedate"
Get-ChildItem -Path $source | Where-Object { $_.Name -match $filter } | Copy-Item -Destination $destination
Run Code Online (Sandbox Code Playgroud)

如果您仍未获得任何结果,那么我们需要查看过滤器本身.

相关资源: