【问题标题】:How to copy **only** the folder content of a directory in powershell script with robocopy如何使用robocopy在powershell脚本中复制**仅**目录的文件夹内容
【发布时间】:2019-10-11 15:48:53
【问题描述】:

我正在尝试复制目录中的文件夹,此问题的答案中提供的脚本会复制目录中的所有内容: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"
}

【问题讨论】:

  • 所以我可以将 robocopy.exe 替换为您的意思吗?
  • 我的意思是你看看robocopy/CREATE 选项(这可能不合适,因为它也会创建零长度文件),或者xcopy 命令及其@ 987654330@ 选项(可能与/E 一起使用),用于在不复制任何文件的情况下创建目标目录树...

标签: powershell robocopy


【解决方案1】:

您希望使用以下参数: /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(' ')

【讨论】:

    【解决方案2】:

    作为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"
    

    【讨论】:

    • 是的,它们都可以正常工作,但 robocopy.exe 稍快
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2022-01-08
    • 2021-01-02
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多