[英]How to copy **only** the folder content of a directory in powershell script with robocopy
我正在嘗試僅復制目錄中的文件夾,此問題的答案中提供的腳本將復制目錄中的所有內容: 如何僅在Powershell中復制帶有robocopy的文件夾?
下面提供了此答案的代碼:
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"
}
您正在尋找使用參數:/ 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(' ')
作為robocopy
的替代方法,您還可以使用xcopy
命令 ,該命令具有選項/T
來創建目標目錄樹,而無需復制任何文件。 這是輸入xcopy /?
時幫助文本的摘錄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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.