powershell .ps1脚本。使用-Contains / -notcontains和Get -Childitem将文件获取文件夹中的文件
我希望此脚本在FTP文件夹中复制文件,只有在Pendientes或Procesados文件夹中都不存在时,它们才会复制文件。但是,每次运行脚本时,都会复制FTP的所有文件。我必须误解一些明显的东西,但无法找到。有什么想法吗?
$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation = 'C:\PENDIENTES'
$FtpLocation = 'C:\FTP'
$Procesados = Get-ChildItem -LiteralPath $ProcesadosLocation -Recurse -File
$Pdtes = Get-ChildItem -LiteralPath $PdtesLocation -Recurse -File
$ftp = Get-ChildItem -LiteralPath $FtpLocation -Recurse -File
ForEach ($File in $ftp)
{
If (($Procesados.Name -notcontains $File.Name) -and ($PdtesLocation.Name -notcontains $File.Name ))
{
Write-Output "New file detected: $($File.FullName)"
Copy-Item -LiteralPath $File.FullName -Destination $PdtesLocation
}
}
I expected this script to copy files in the FTP folder, only if they don't exist neither in the Pendientes or Procesados Folders. However, every time I run the script, it copies all files from FTP. I must be mising something obvious, but unable to find out. Any ideas?
$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation = 'C:\PENDIENTES'
$FtpLocation = 'C:\FTP'
$Procesados = Get-ChildItem -LiteralPath $ProcesadosLocation -Recurse -File
$Pdtes = Get-ChildItem -LiteralPath $PdtesLocation -Recurse -File
$ftp = Get-ChildItem -LiteralPath $FtpLocation -Recurse -File
ForEach ($File in $ftp)
{
If (($Procesados.Name -notcontains $File.Name) -and ($PdtesLocation.Name -notcontains $File.Name ))
{
Write-Output "New file detected: $($File.FullName)"
Copy-Item -LiteralPath $File.FullName -Destination $PdtesLocation
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您可以简化代码的一种方法:
Here is one way you could simplify your code:
由于
-path
和-literalPath
get-childitem kan的参数都采用一系列路径,因此您可以将文件名组合起来以使用一个呼叫来排除文件名来get-childitem 。然后,借助
where-object
子句,应该很容易复制在both paths中不存在的文件:Since both the
-Path
and-LiteralPath
parameters of Get-ChildItem kan take an array of paths, you can combine the file names to exclude using just one call to Get-ChildItem.Then, with the help of a
Where-Object
clause it should be easy enough to only copy files that do not exist in bothg paths: