【发布时间】:2020-03-30 18:34:03
【问题描述】:
在编写一个 PS 脚本以定期将文件从一个文件夹移动到另一个文件夹以进行处理时,我需要帮助。我已经使用了下面提到的代码,但我不知道如何检查文件是否已经存在于目标中。它存在,则不应复制该文件。请帮忙。
$destination = "C:\Users\User\Desktop\Destination\"
$source = "C:\Users\User\Desktop\Source"
$files = Get-ChildItem $source
Write-Host $files
foreach($file in $files)
{
$path = "C:\Users\User\Desktop\Source\" + $file
Move-Item -Path $path -Destination $destination -Force
}
请帮助检查文件是否在目标文件夹中并跳过它移动。
问候,
米特什·阿格拉瓦尔
【问题讨论】:
-
不确定这是否是重复的,但似乎您可以使用
Test-Path确保在移动之前文件不存在。类似于if (-not(Test-Path -Path $path)){ Move-Item -Path $path -Destination $destination}。 -
使用 robocopy 可能会更容易
标签: powershell