【发布时间】:2018-04-21 18:20:26
【问题描述】:
我正在尝试将文件从源文件夹复制到目标文件夹,并在此过程中重命名文件。
$Source = "C:\Source"
$File01 = Get-ChildItem $Source | Where-Object {$_.name -like "File*"}
$Destination = "\\Server01\Destination"
Copy-Item "$Source\$File01" "$Destination\File01.test" -Force -
Confirm:$False -ErrorAction silentlyContinue
if(-not $?) {write-warning "Copy Failed"}
else {write-host "Successfully moved $Source\$File01 to
$Destination\File01.test"}
问题是,如果找不到文件,Get-ChildItem 不会抛出错误消息,而只是给你一个空白,如果没有名为 @ 的文件,我最终会在目标中找到一个名为 File01.test 的文件夹987654324@ 存在于$Source 中。
如果确实存在,则复制操作执行得很好。但是,如果$Source 中不存在匹配的文件,我不希望创建文件夹,而我只想在日志文件中记录一条错误消息,并且不会发生文件操作。
【问题讨论】:
标签: powershell copy rename get-childitem