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