Copy-Item无法复制到正确的目标目录

时间:2019-05-24 11:02:55

标签: powershell copy-item

我正在尝试使用PowerShell脚本复制目录结构。我希望排除某个目录。

我的目录结构如下:

C:\
└───murks
    ├───murks_source
    │   │   test1.txt
    │   │   test2.txt
    │   │
    │   ├───sub
    │   │       subtest1.txt
    │   │       subtest2.txt
    │   │       subtest3.txt
    │   │
    │   └───sub2
    │           sub2test.txt
    │
    └───murks_target

现在,当我运行脚本时,它不会使用下级文件创建子目录“ sub2”。相反,它将所有文件(也包括“ sub2”子目录的文件)直接复制到murks_target_directory中。我不了解这种行为,因为到目前为止“ Select -ExpandProperty FullName”部分看起来不错。

任何帮助或提示都将受到高度赞赏。 预先感谢!

到目前为止,我的脚本看起来像这样:

$rootFolderPath = 'C:\murks\murks_source'
$excludeDirectories = ("sub");

function Exclude-Directories
{
    process
    {
        $allowThrough = $true
        foreach ($directoryToExclude in $excludeDirectories)
        {
            $directoryText = "*\" + $directoryToExclude
            $childText = "*\" + $directoryToExclude + "\*"
            if (($_.FullName -Like $directoryText -And $_.PsIsContainer) `
                -Or $_.FullName -Like $childText)
            {
                $allowThrough = $false
                break
            }
        }

        if ($allowThrough)
        {
            return $_
        }
    }
}

Get-ChildItem $rootFolderPath -Recurse | Exclude-Directories | Select -ExpandProperty FullName | Copy-Item -Destination C:\murks\murks_target -Force

3 个答案:

答案 0 :(得分:0)

几乎找到了可以提供您不满意的功能的答案。

$from = "C:\murks\murks_source"
$to = "C:\murks\murks_target"
$exclude = Get-ChildItem -Path C:\murks\murks_source\sub2\ -Depth 10
Copy-Item $from -Exclude $exclude -Destination $to -Recurse

它将忽略除文件夹结构以外的所有文件。 文件夹结构将被复制但为空。

希望有帮助。

答案 1 :(得分:0)

下面的自定义函数应执行您想要的操作:

function Copy-Path {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [ValidateScript({Test-Path -Path $_ -PathType Container})]
        [string]$Source,

        [Parameter(Position = 1)]
        [string]$Destination,

        [string[]]$ExcludeFolders = $null,
        [switch]$IncludeEmptyFolders
    )
    $Source      = $Source.TrimEnd("\")
    $Destination = $Destination.TrimEnd("\")

    Get-ChildItem -Path $Source -Recurse | ForEach-Object {
        if ($_.PSIsContainer) {
            # it's a folder
            if ($ExcludeFolders.Count) {
                if ($ExcludeFolders -notcontains $_.Name -and $IncludeEmptyFolders) {
                    # create the destination folder, even if it is empty
                    $target = Join-Path -Path $Destination -ChildPath $_.FullName.Substring($Source.Length)
                    if (!(Test-Path $target -PathType Container)) {
                        Write-Verbose "Create folder $target"
                        New-Item -ItemType Directory -Path $target | Out-Null
                    }
                }
            }
        }
        else {
            # it's a file
            $copy = $true
            if ($ExcludeFolders.Count) {
                # get all subdirectories in the current file path as array
                $subs = $_.DirectoryName.Replace($Source,"").Trim("\").Split("\")
                # check each sub folder name against the $ExcludeFolders array
                foreach ($folderName in $subs) {
                    if ($ExcludeFolders -contains $folderName) { $copy = $false; break }
                }
            }

            if ($copy) {
                # create the destination folder if it does not exist yet
                $target = Join-Path -Path $Destination -ChildPath $_.DirectoryName.Substring($Source.Length)
                if (!(Test-Path $target -PathType Container)) {
                    Write-Verbose "Create folder $target"
                    New-Item -ItemType Directory -Path $target | Out-Null
                }
                Write-Verbose "Copy file $($_.FullName) to $target"
                $_ | Copy-Item -Destination $target -Force
            }
        }
    }
}

就位,像这样使用它:

Copy-Path -Source 'C:\murks\murks_source' -Destination 'C:\murks\murks_target' -ExcludeFolders 'sub' -Verbose

其中参数-ExcludeFolders可以是要跳过的文件夹名称数组。
添加开关参数IncludeEmptyFolders还将在目标位置创建空文件夹。如果遗漏,将不会复制空文件夹。

使用示例文件结构,结果为

C:\murks\murks_target
|   test1.txt
|   test2.txt
|
\---sub2
        sub2test.txt

答案 2 :(得分:0)

这些都可以通过单线实现。

$source = 'C:\murks\murks_source'
$dest = 'C:\murks\murks_target'

复制除“ Sub”文件夹及其内容以外的所有内容:

Get-ChildItem -Path $source -Recurse | ? {$_.Name -notmatch 'sub'} | Copy-Item -Destination $dest

保留包括所有文件的文件夹结构:

Get-ChildItem -Path $source | Copy-Item -Destination $dest -Recurse -Container

复制文件夹结构但不复制文件...

Get-ChildItem -Path $source | ? {$_.PSIsContainer} | Copy-Item -Destination $dest -Recurse -Exclude '*.*'

您显然可以将示例1和示例2结合起来以保留文件夹/文件结构,还可以通过名称,通配符等排除所需的任何目录。

相关问题