我怎样才能在powershell中使用robocopy复制文件夹?

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

如何仅使用powershell和robocopy复制目录中的文件夹?

Get-ChildItem 'C:\temp\test' |

ForEach-Object {
    $newname = ($_.BaseName -Replace '[^\x20-\x5D,\x60-\x7E]+', '-')
    write-host $_.BaseName
    write-host $newname
    robocopy.exe 'C:\temp\test\'$_.BaseName 'C:\temp\test\copy\'$newname
}

编辑

谢谢效果很好

Get-ChildItem 'C:\temp\test' |

ForEach-Object {
    $newname = ($_.BaseName -Replace '[^\x20-\x5D,\x60-\x7E]+', '-')
    if (($_.GetType()).Name -eq "DirectoryInfo"){
        write-host "folder"
    }
    write-host $_.BaseName
    write-host $newname
    robocopy.exe "C:\temp\test\$($_.BaseName)" "C:\temp\test\copy\$newname"
}
powershell robocopy
2个回答
0
投票

修正了错误,修改了以下代码。

Get-ChildItem 'C:\temp\test' |

ForEach-Object {
    $newname = ($_.BaseName -Replace '[^\x20-\x5D,\x60-\x7E]+', '-')
    write-host $_.BaseName
    write-host $newname
    robocopy.exe "C:\temp\test\$($_.BaseName)" "C:\temp\test\copy\$newname"
}

0
投票

你可以这样做:

$items = Get-ChildItem 'C:\temp\test'

foreach($item in $items){
    if(($item.GetType()).Name -eq "DirectoryInfo"){
        $newname = ($item.BaseName -Replace '[^\x20-\x5D,\x60-\x7E]+', '-')
        Write-Host $item.BaseName
        Write-Host $newname
        robocopy.exe "C:\temp\test\$($_.BaseName)" "C:\temp\test\copy\$newname"
    }else{
        Write-Host "$item is not a directory"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.