【问题标题】:Copy-Item not including subfolder files of source directory in PowerShellCopy-Item 不包括 PowerShell 中源目录的子文件夹文件
【发布时间】:2017-04-13 12:20:28
【问题描述】:

我有一个脚本,可以将具有特定扩展名的文件从源目录移动到目标目录。

我的源目录如下:

文件文件夹

  • File1.td

  • 文件2.td

    子文件夹

    • File3.td

    • File4.td

我的脚本很短,看起来像:

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source- or destination path is incorrect, please check "
    break
}else
{
    Write-Host "Success"
    Copy-Item -path $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath -Recurse
}

我必须提到 $SourceDirPath 来自配置文件,并且只有在我将其声明为 C:\FILESFOLDER\* 时才有效

脚本有效,但不会将文件从子文件夹复制到目标。目的地只有 File1 和 File2。

我的 Copy-Item 命令有什么问题?

【问题讨论】:

  • 您想复制子文件夹中的文件和子文件夹本身吗?或者你只想要文件?
  • 我想在目的地有完全相同的结构,所以子文件夹和文件

标签: powershell copy-item file-move


【解决方案1】:

-Recurse 开关对您的情况无效,因为它仅适用于由 $SourceDirPath 组合匹配的项目,它使用尾随 \* 来匹配项目 在源目录和-Filter,在您的情况下,它们只是位于源目录中直接*.td文件。

省略结尾的 \* 来定位目录本身(例如,C:\FOLDER 而不是 C:\FOLDER\*),原则上解决了这个问题,但是,@ 987654321@ 只有在目标目录存在时才能按预期工作。 如果确实存在,则将项目放置在目标目录的子目录中,以目录命名。

如果在复制之前现有目标目录中没有需要保留的内容(并且目录本身没有特殊属性需要保留,您可以通过删除解决此问题> 预先存在的目标目录

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source or destination path is incorrect, please check "
    break
}
else
{
    Write-Host "Success"

    # Delete the preexisting destination directory,
    # which is required for the Copy-Item command to work as intended.
    # BE SURE THAT IT IS OK TO DO THIS.
    Remove-Item $DestinationDirPath -Recurse

    # Note: $SourceDirPath must NOT end in \*  
    Copy-Item -Recurse -LiteralPath $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath
}

如果您确实需要保留现有的 $DestinationDirPath 内容或属性(ACL、...),则需要做更多的工作。

【讨论】:

  • 很遗憾,它没有工作,它还删除了我的整个目标文件夹?
  • @KahnKah:如前所述(如果我可以在答案中更清楚地说明这一点,请告诉我):解决方法要求删除目标文件夹首先(我知道这很烦人——Copy-Item 在很多方面都被破坏了)。但是,除此之外,它应该可以工作。所以:你是说它不起作用,还是说你必须保留现有目标目录中的内容?
【解决方案2】:

因此,这将比较目录源与目标,然后复制内容。忽略我的时髦变量,我在下面修改了它们

 #Release folder
    $Source =  ""
    #Local folder
    $Destination = ""

    #Find all the objects in the release
    get-childitem $Source -Recurse | foreach {

    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5 # Obtain hash

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination #Escape the hash
    Write-Host "comparing $SrcFile to $DestFile" -ForegroundColor Yellow

    if (Test-Path $DestFile) 
    {
    #Check the hash sum of the file copied
    $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5

    #compare them up
    if ($SrcHash.hash -ne $DestHash.hash) {
        Write-Warning "$SrcFile and $DestFile Files don't match!"

        Write-Warning "Copying $SrcFile to $DestFile " 

        Copy-Item $SrcFile -Destination $DestFile -Force
    } 
    else {
        Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor 
Green

    }
        } 
    else {
        Write-host "$SrcFile is missing! Copying in" -ForegroundColor Red
        Copy-Item $SrcFile -Destination $DestFile -Force

         }
    }  

【讨论】:

  • 不行,你只是添加了-Force并改变了开关的位置?我的目标文件夹中仍然只有 2 个文件
  • 您更新的答案无效。它说`Copy-Item:无法将参数绑定到参数'Path',因为它是空的。``
  • 这是因为我在复制和粘贴时遗漏了一个变量。我必须在一分钟内运行,但我在上面添加了一个脚本,它对对象进行二进制比较,然后回避副本。这将做你想要的,但也会做一个二进制比较并报告对 Ps 的颜色编码引用。这是一种非常冗长的方式来做你想做的事,但会去做!
  • 确实如此,如果它们不同的话。在您的情况下,这将是您更新的唯一原因(如果您有我的想法) - MD5 算法非常适合,因为它检查实际的文件哈希以及内容。这是一个全面的检查,而不是一些“哦,这是同一天”然后继续前进。
  • 我刚刚意识到,如果文件之间存在差异,它确实会这样做。再次感谢这段伟大的代码
【解决方案3】:

您应该使用Get-ChildItem cmdlet 和-recurse 参数根据您的过滤条件检索所有文件。然后,您可以将结果通过管道传输到 Copy-Item cmdlet,并且只需指定一个目标。

【讨论】:

  • 这是否也让我在源目录和目标目录中都有完全相同的结构?
  • 您必须自己处理。您可以考虑使用 Robocopy。
  • 不完全,这是可能的,你想在复制前比较还是只复制所有内容而不管目的地是否存在?
  • 我已经告诉过你,你必须自己注意结构。此外,robocopy 自带 windows,它不是外部工具。
  • 好的,谢谢
【解决方案4】:

试试这个:

$source =  "C:\Folder1"

$destination = "C:\Folder2"

$allfiles = Get-ChildItem -Path $source -Recurse -Filter "*$extension"
foreach ($file in $allfiles){
    if (test-path ($file.FullName.replace($source,$destination))) {
    Write-Output "Seccess"
    Copy-Item -path $file.FullName -Destination ($file.FullName.replace($source,$destination))
    }
    else {
    Write-Output "The source- or destination path is incorrect, please check "
    break
    }
}

【讨论】:

  • 如果我的来源没有\*,我的脚本将无法工作
猜你喜欢
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
相关资源
最近更新 更多