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

How can i only copy folders with robocopy in powershell?

如何使用 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"
}

更正了错误,修改了下面的代码。

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"
}

你可以这样做:

$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"
    }
}