我尝试仅复制 目录中的文件夹,此问题答案中提供的脚本将复制目录中的所有内容:How can i only copy folders with robocopy in powershell?
下面提供了此答案的代码:
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"
}
答案 0 :(得分:3)
您正在寻找使用参数: / E =复制目录,即使为空 / XF 。 =排除所有文件,即带有“。”的任何文件。在文件名中,例如所有文件
使用示例:
$Source = "C:\temp\test"
$Destination = "C:\temp\test2"
$robocopyOptions = "/E /XF *.*"
# filelist is required but we will ignore it anyway with parameters we are passing
$fileList = ''
robocopy.exe $Source $Destination $fileList $robocopyOptions.split(' ')
答案 1 :(得分:1)
作为robocopy
的替代方法,您还可以使用xcopy
command,它具有选项/T
来创建目标目录树而不复制任何文件。当您在命令提示符窗口中输入xcopy /?
时,以下是帮助文本的摘录:
/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
因此您可以使用以下命令行:
xcopy.exe /T /E /I "C:\temp\test\folder" "C:\temp\test\copy"