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