将项目文件夹复制到子文件夹

时间:2020-07-03 14:08:28

标签: powershell

我具有以下文件夹结构 C:\ folder \ item .. C:\ folder \ item2 ..

我想将C \ folder的内容复制到C \ folder \ copy

$sourcePath = 'C:\folder'
$destPath = 'C:\folder\copy'
Get-ChildItem -Path $sourcePath -Recurse  | Copy-Item -Destination {Join-Path $destPath $_.FullName.Substring($sourcePath.Length) } -Force -Recurse  

找不到路径的返回值。

编辑,以为我最终成功了

function Copy-Folder{
    Param
    (
        [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false,
        HelpMessage="source folder")]
        [System.String]
        [ValidateScript({Test-Path $_ })]
        $sourcePath,
        [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false,
        HelpMessage="destination folder.")]
        [System.String]
        $destPath
    )

    $items = Get-ChildItem -Path $sourcePath -Recurse  -Exclude $destPath
    
    foreach ($item in $items) 
    {
        $d = Join-Path -Path $destPath -ChildPath $item.FullName.Substring($sourcePath.Length)
        if ($item.PSIsContainer -eq $false) {New-Item -Path $d -ItemType File -Force}     
        Copy-Item -Path $item.FullName -Destination $d -Force  
    }
}

0 个答案:

没有答案