Robocopy: copy files preserving folder structure but adding a subfolder [closed]

自作多情 提交于 2019-12-13 15:54:50

问题


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. For the following source folder:

    --Logs
    ----Application
    ------Service
    --------Log1.txt
    --------Log2.txt
    ------WebSite
    --------Log1.txt
    --------Log2.txt
    
  2. Destination folder should be created in the following way:

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

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

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

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.


回答1:


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
}


来源:https://stackoverflow.com/questions/21606259/robocopy-copy-files-preserving-folder-structure-but-adding-a-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!