PowerShell脚本不复制子文件夹及其内容

问题描述 投票:0回答:1

这是我第一次使用自定义Powershell脚本,我想我可能会丢失一些东西。一切正常,除了不复制子文件夹及其内容。


$sourcePath = 'C:\Users\User\Desktop\test\test1'
$destinationPath = 'C:\Users\User\Desktop\test\test2'
$files = Get-ChildItem -Path $sourcePath -Recurse
$filecount = $files.count
$i=0
Foreach ($file in $files) {
    $i++
    Write-Progress -activity "Moving files..." -status "($i of $filecount) $file" -percentcomplete (($i/$filecount)*100)

    # Determine the absolute path of this object's parent container.  This is stored as a different attribute on file and folder objects so we use an if block to cater for both
    if ($file.psiscontainer) {$sourcefilecontainer = $file.parent} else {$sourcefilecontainer = $file.directory}

    # Calculate the path of the parent folder relative to the source folder
    $relativepath = $sourcefilecontainer.fullname.SubString($sourcepath.length)

    # Copy the object to the appropriate folder within the destination folder
    copy-Item $file.fullname ($destinationPath + $relativepath)
}

我确定这与Path而不是未正确使用的Root或Recurse选项有关。

[如果有人可以帮助我,将不胜感激。

谢谢!

powershell copy paste
1个回答
0
投票

要递归复制,请使用

复制项-路径-目的地-递归

请参见Official documentation

© www.soinside.com 2019 - 2024. All rights reserved.