Robocopy:复制文件保留文件夹结构,但添加一个子文件夹 [英] Robocopy: copy files preserving folder structure but adding a subfolder

查看:1033
本文介绍了Robocopy:复制文件保留文件夹结构,但添加一个子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将包含带有文件的子文件夹的文件夹复制到另一台计算机上的共享文件夹。

I need to copy a folder which contains subfolders with files to a shared folder on another machine. During that the script should create a custom sub folder in each destination folder and place files there.

我希望这个例子能够完成这个任务:

I hope this example clatifies the task:


  1. 对于以下源文件夹:

--Logs
----Application
------Service
--------Log1.txt
--------Log2.txt
------WebSite
--------Log1.txt
--------Log2.txt


  • 目标文件夹应以以下方式创建: >

  • Destination folder should be created in the following way:

    --Logs
    ----Application
    ------Service
    --------Machine1
    ----------Log1.txt
    ----------Log2.txt
    ------WebSite
    --------Machine1
    ----------Log1.txt
    ----------Log2.txt
    


  • 正如您所看到的底层子文件夹 Machine1 已创建。
    你能提供一个PowerShell脚本的例子来执行吗?

    As you see at the bottom level subfolders Machine1 have been created. Can you please provide an example of PowerShell script which performes that?

    我在ROBOCOPY命令中调查输入参数。它似乎不允许这样做直接。

    I investigated input parameters in ROBOCOPY command. It seems it doesn't allow to do that straightforward.

    也知道我可以symply遍历所有文件夹结构,创建所需的子文件夹和复制文件到每个子文件夹。但它似乎是一个太长的方式。所以我想知道如果我错过任何东西。

    Also I know that I can symply iterate through all folder structure, create required sub folders and copy files to each subfolder. But it seems to be a too 'long' way. So I want to know if I'm missing anything. Maybe there is a better smart way to do that.

    推荐答案

    我建议使用 robocopy 用于复制子文件夹的内容:

    I'd suggest to use robocopy for replicating the contents of the subfolders:

    $srcBase = 'C:\some\Logs\Application'
    $dstBase = 'D:\other\Logs\Application'
    
    Get-ChildItem $srcBase | ? { $_.PSIsContainer } | % {
      $dst = "$dstBase\$($_.Name)\Machine1"
    
      & robocopy $_.FullName $dst /e
    }
    

    这篇关于Robocopy:复制文件保留文件夹结构,但添加一个子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆