【发布时间】:2019-02-15 15:11:49
【问题描述】:
如何使用 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"
}
【问题讨论】:
-
您基本上是将
BaseName重命名为Copy并复制C:\Temp\Test下的所有内容(其中还包含copy文件夹以及再次copy)。所以copy每次运行脚本时都会有它自己的副本。
标签: powershell robocopy