Question

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.

Was it helpful?

Solution

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top