【问题标题】:Copy-Item Exclude one dirCopy-Item 排除一个目录
【发布时间】:2019-12-02 07:46:14
【问题描述】:

我想复制文件夹的内容并排除“Cookies”。

我已经尝试了一些针对类似问题提供的解决方案,但它们对我不起作用。

$excludes = "Cookies"
New-Item -Path $newdir -Type Directory -Name "AppData"
Copy-Item -Path (Get-Item -Path $path"\AppData\*" -Exclude ($excludes)).FullName -Destination $newdir"\AppData" -Recurse -Force

我只想复制目录的内容,不包括 1 个文件夹。

我正在使用 PowerShell V5.1

【问题讨论】:

  • 在我的 win7ps5.1 设置中,-Exclude-Include 参数仅对文件对象的 .Name 部分起作用。这意味着目录名称将永远不会被排除/包含,因为它位于文件对象的.DirectoryName 部分。您需要使用 Where-Object 过滤掉 dir 对象。 [叹息...]

标签: powershell scripting


【解决方案1】:

我写这个是为了日常使用,并打包在脚本模块中,它维护了所有的目录结构并支持通配符:

function Copy-Folder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String]$FromPath,

        [Parameter(Mandatory)]
        [String]$ToPath,

        [string[]] $Exclude
    )

    if (Test-Path $FromPath -PathType Container) {
        New-Item $ToPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
        Get-ChildItem $FromPath -Force | ForEach-Object {
            # avoid the nested pipeline variable
            $item = $_
            $target_path = Join-Path $ToPath $item.Name
            if (($Exclude | ForEach-Object { $item.Name -like $_ }) -notcontains $true) {
                if (Test-Path $target_path) { Remove-Item $target_path -Recurse -Force }
                Copy-Item $item.FullName $target_path
                Copy-Folder -FromPath $item.FullName $target_path $Exclude
            }
        }
    }
}

只需拨打Copy-Folder -FromPath 'fromDir' -ToPath 'destDir' -Exclude Cookies

-FromPath-ToPath 可以省略,

Copy-Folder fromDir destDir -Exclude Cookies

【讨论】:

    【解决方案2】:
    $destName = "C:\Test Folder"
    $target = "Folder"
    $exclude = "Bad Folder"
    
    Get-ChildItem $target | Where-Object {$_.BaseName -notin $exclude} | Copy-Item -Destination $destName -Recurse -Force
    

    这应该可行。如果要排除多个文件夹或文件,可以将 exclude 设置为多个名称。如果您只需要文件,则需要将 -Directory 添加到 Get-ChildItemCopy-Item and exclude folders我基于这个之前的回答

    【讨论】:

      【解决方案3】:

      此代码无效Get-Item -Path $path"\AppData\*",PowerShell 无法连接变量和字符串。将代码更改为:

      $excludes = "Cookies"
      New-Item -Path $newdir -Type Directory -Name "AppData"
      
      # Join the path correctly
      $joinedPath = Join-Path $path "AppData\*"
      Copy-Item -Path (Get-Item -Path $joinedPath -Exclude ($excludes) -Directory).FullName -Destination $newdir"\AppData" -Recurse -Force
      

      仅供参考:请注意,-Exclude 开关仅在路径中包含通配符时才有效(在问题中正确完成)。 Source:

      -排除

      以字符串数组的形式指定此 cmdlet 在操作中排除的一个或多个项目。此参数的值限定了 Path 参数。输入路径元素或模式,例如 *.txt。允许使用通配符。 Exclude 参数仅在命令包含某个项目的内容时有效,例如 C:\Windows*,其中通配符指定 C:\Windows 目录的内容。

      希望对您有所帮助。

      【讨论】:

      • 它一直被复制:C
      • 这是因为“Cookies”是一个子目录吗?它在漫游\Microsoft\Cookies 中。
      • 尝试在Get-ChildItem 处添加-Directory 开关,否则,cmdlet 将返回排除目录中的文件。
      • 我试过这个 Get-ChildItem $joinedPath -Directory | Where-Object{$_.Name -notin $excludes} | Copy-Item -Destination $newdir"\AppData\Roaming\" -Recurse - Force Sry idk 我如何将其格式化为代码:S PS。它没有用,我仍然复制目录:C
      • 好吧,我已经放弃了,我现在用 /XD 参数使用 robocopy
      猜你喜欢
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多