powershell .ps1脚本。使用-Co​​ntains / -notcontains和Get -Childitem将文件获取文件夹中的文件

发布于 2025-01-23 04:19:29 字数 740 浏览 0 评论 0原文

我希望此脚本在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2025-01-30 04:19:30

这是您可以简化代码的一种方法:

$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation = 'C:\PENDIENTES'
$FtpLocation = 'C:\FTP'

$ftp = Get-ChildItem -LiteralPath $FtpLocation -Recurse -File
# combine boths arrays of values in one
$excludeMe = @(
    Get-ChildItem -LiteralPath $ProcesadosLocation -Recurse -File
    Get-ChildItem -LiteralPath $PdtesLocation -Recurse -File
).Name

foreach($file in $ftp) {
    if($file.Name -in $excludeMe) {
        # if this is `$true`, just go next
        continue
    }
    Write-Output "New file detected: $($File.FullName)"
    Copy-Item -LiteralPath $file.FullName -Destination $PdtesLocation
}

Here is one way you could simplify your code:

$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation = 'C:\PENDIENTES'
$FtpLocation = 'C:\FTP'

$ftp = Get-ChildItem -LiteralPath $FtpLocation -Recurse -File
# combine boths arrays of values in one
$excludeMe = @(
    Get-ChildItem -LiteralPath $ProcesadosLocation -Recurse -File
    Get-ChildItem -LiteralPath $PdtesLocation -Recurse -File
).Name

foreach($file in $ftp) {
    if($file.Name -in $excludeMe) {
        # if this is `$true`, just go next
        continue
    }
    Write-Output "New file detected: $($File.FullName)"
    Copy-Item -LiteralPath $file.FullName -Destination $PdtesLocation
}
人生戏 2025-01-30 04:19:30

由于-path-literalPath get-childitem kan的参数都采用一系列路径,因此您可以将文件名组合起来以使用一个呼叫来排除文件名来get-childitem 。

然后,借助where-object子句,应该很容易复制在both paths中不存在的文件:

$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation      = 'C:\PENDIENTES'
$FtpLocation        = 'C:\FTP'

$exclude = (Get-ChildItem -LiteralPath $ProcesadosLocation, $PdtesLocation -Recurse -File).Name

Get-ChildItem -LiteralPath $FtpLocation -Recurse -File |
Where-Object { $exclude -notcontains $_.Name } |
ForEach-Object {
    Write-Output "New file detected: $($_.FullName)"
    Copy-Item -LiteralPath $_.FullName -Destination $PdtesLocation    
}

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:

$ProcesadosLocation = 'C:\PROCESADOS'
$PdtesLocation      = 'C:\PENDIENTES'
$FtpLocation        = 'C:\FTP'

$exclude = (Get-ChildItem -LiteralPath $ProcesadosLocation, $PdtesLocation -Recurse -File).Name

Get-ChildItem -LiteralPath $FtpLocation -Recurse -File |
Where-Object { $exclude -notcontains $_.Name } |
ForEach-Object {
    Write-Output "New file detected: $($_.FullName)"
    Copy-Item -LiteralPath $_.FullName -Destination $PdtesLocation    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文