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:
-
对于以下源文件夹:
--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
}