如何使用 robocopy 在 powershell 脚本中复制**仅**目录的文件夹内容

How to copy **only** the folder content of a directory in powershell script with 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 command,它具有一个选项 /T,可以在不复制任何文件的情况下创建目标目录树。以下是在命令提示符 window:

中输入 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"