【问题标题】:How to copy a file to multiple folders in PowerShell如何将文件复制到 PowerShell 中的多个文件夹
【发布时间】:2014-07-10 14:57:42
【问题描述】:

我正在运行一个脚本,其中包含多个环境,可以从弹出的窗口中进行选择。我遇到的唯一问题是当我想设置脚本以从我创建的源函数复制并一次将其放入多个位置时。

我需要帮助的代码部分发布在下面。

$Source = Select-Boss

$destination = 'D:\parts','D:\labor','D:\time','D:\money'

"Calling Copy-Item with parameters source: '$source', destination: '$destination'."

Copy-Item -Path $source -Destination $destination

下面的部分是如何在脚本中设置其余的复制功能,因此您可以更好地了解主要部分复制是什么。

$Source = Select-Boss

$destination = 'D:\parts'

"Calling Copy-Item with parameters source: '$source', destination: '$destination'."

Copy-Item -Path $source -Destination $destination

但是对于一个特定的部分,我需要将其复制到多个位置。我需要这样做,因为我不必更改我登录的服务器并转到另一个服务器。这一切都在一个位置完成,我希望让事情变得更容易,而不是编写一大堆小代码去复制并保存在文件夹中。

【问题讨论】:

    标签: powershell powershell-3.0 powershell-ise


    【解决方案1】:

    copy-item-destination 参数只接受一个值,因此您需要某种类型的循环。

    假设您想在多个文件夹中使用相同的文件名:

    $destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }
    

    应该这样做。

    【讨论】:

    • 我会把那部分放在哪里。我确实想在多个文件夹中保留相同的文件名
    • 嘿,我收到了这个错误 Join-Path : Cannot bind argument to parameter 'Path' because it is null.,有什么想法吗?
    • @DhartiSutariya 检查你的输入——输入集合是空的吗? – 但实际上这应该是一个新问题,您可以在其中展示完整的重新创建。
    【解决方案2】:

    我认为你想要的是这样的:

    $Source = Select-Boss
    
    $destination = @("D:\parts","D:\labor","D:\time","D:\money")
    
    # Calling Copy-Item with parameters source: '$source', destination: '$destination'."
    
    foreach ($dir in $destination)
    {
        Copy-Item -Path $source -Destination $dir
    }
    

    这段代码创建了一个文件夹数组,然后遍历每个文件夹,将您的文件复制到其中。

    【讨论】:

    • 我进行了您建议的更改,但它不起作用。我知道的是Copy-Item : The given path's format is not supported. At line:9 char:22 + Copy-Item -Path $source -Destination $dir + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
    • $source的值是多少?
    • $source 的值为 \\ntsrv\common\Deployments\Boss\testing.txt
    • select-boss 是我创建的一个函数,它打开一个窗口,允许我转到一个文件夹并在其中选择一个文件,然后在选择后存储结果
    • 我想在最后一个目的地复制时排除一些文件,有什么办法吗?
    【解决方案3】:

    https://www.petri.com/use-powershell-to-copy-files-to-multiple-locations

    dir c:\work\*.txt | copy-item -Destination $destA -PassThru | copy -dest $destB -PassThru
    

    【讨论】:

      【解决方案4】:

      这是我用过的最好最简单的解决方案。

      在我的例子中,它是一个网络位置,但它也可以用于本地系统。

      "\\foo\foo" 位置按用户名包含 10 个文件夹。 # 使用双斜线(StackOverflow 上没有显示)

      dir "\\foo\foo\*" | foreach-object { copy-item -path d:\foo -destination $_ } -verbose
      

      您必须对网络共享和目标文件夹具有写入权限。

      【讨论】:

        【解决方案5】:
        # Copy one or more files to another directory and subdirectories
        
        $destinationFrom = "W:\_server_folder_files\basic"
        $typeOfFiles = "php.ini", "index.html"
        $filesToCopy = get-childitem -Path $destinationFrom -Name -include $typeOfFiles -Recurse
        $PathTo = "W:\test\"
        $destinationPath =  Get-ChildItem -path $PathTo -Name -Exclude "*.*" -recurse -force
        
        foreach ($file_item in $filesToCopy)
        {
            #Remove-Item -Path (Join-Path -Path $PathTo -ChildPath $file_item)
            Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination $PathTo
            # Write-Verbose (Join-Path -Path $destinationFrom -ChildPath $file_item) -verbose
        
            ForEach($folder in $destinationPath)
            {
                #Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item)
                #Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination (Join-Path -Path $PathTo -ChildPath $folder)
                # Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-06
          • 2013-10-29
          • 1970-01-01
          • 2019-08-06
          • 2015-07-14
          • 2018-10-12
          • 1970-01-01
          相关资源
          最近更新 更多