copy subfolders with specific names to same structure on different server

Welcome Forums General PowerShell Q&A copy subfolders with specific names to same structure on different server

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 weeks, 6 days ago.

  • Author
    Posts
  • #132002

    Participant
    Points: 17
    Rank: Member

    Hi,

    I try to copy subfolders with specific name formats to another server while maintaining the folder structure. I think I'm almost there, but it keeps copying the folders to the literal -destination root path, and I want the folders to be copied to the subfolders they were already under. The structure is already in place, I just want to replace the content in the destination with the copied content.

    Source folders: any subfolder and their content within \\source\folder$\folders\ where the name ends with 1*A (where * is a random character)

    Destination: the exact same location, except on the \\destination server. So that would be \\destination\folder$\folders\

    So for example, the folder and it content from \\source\folder$\folders\123456\123456_17A should be copied to \\destination\folder$\folders\123456\123456_17A

     

    So far I have this:

    $DestBase = '\\destination\folder$\folders'
    Get-ChildItem -Path \\source\folder$\folders -Include *A -Directory -Recurse -Force | 
    Copy-Item -Destination {$($_.Fullname) -replace '\\source\folder$\folders\\',"$DestBase"} -Recurse -Force -Whatif

     

    The error that i get:

    "Copy-Item : Cannot overwrite the item \\source\folder$\folders\123456\123456_17A with itself."

    What am i doing wrong?

    Thank in advance,

     

    Dj

  • #132009

    Participant
    Points: 282
    Helping Hand
    Rank: Contributor

    Hi Djurre,

    Try this...

    $SourcePath = '\\Share\Folders\'
    $DestPath = '\\Server01\Folder\'
    $ListDirectories = Get-ChildItem -Path $SourcePath -Recurse -Directory -Filter "*A"
    foreach ($Directory in $ListDirectories)
    {
    $DestDir=$DestPath+$Directory.FullName.ToString().Replace($SourcePath,")
    New-Item-Path $DestDir-ItemType Directory |Out-Null
    }
  • #132039

    Participant
    Points: 17
    Rank: Member

    Hi Kiran,

    Thanks for the reply. If I execute, I get the following error:

    New-Item : A positional parameter cannot be found that accepts argument 'Directory'.
    At line:7 char:1
    + New-Item -Path $DestDir-ItemType Directory |Out-Null
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
    
    

    I tried to Google it, but hit a wall :(.

    Thanks,

    Djurre

  • #132054

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

    there is a space required

    #Wrong
    $DestDir-ItemType Directory
    
    #Correct
    $DestDir -ItemType Directory
    
  • #132171

    Participant
    Points: 282
    Helping Hand
    Rank: Contributor

    Yes, as @kvprasoon mentioned, it needs space.

    I don't why syntax highlighting is not properly formatting.

    
    $SourcePath = '\\Share\Folders\'
    $DestPath = '\\Server01\Folder\'
    $ListDirectories = Get-ChildItem -Path $SourcePath -Recurse -Directory -Filter "*A"
    foreach ($Directory in $ListDirectories)
    {
    $DestDir = $DestPath + $Directory.FullName.ToString().Replace($SourcePath,"")
    New-Item -Path $DestDir -ItemType Directory |Out-Null
    }
    
    
  • #132272

    Participant
    Points: 17
    Rank: Member

    Happy newyear everybody and thanks for the replies. It now returns that the path already exists, however I found another solution:

    I generated a list with all the foldernames. In Excel I used a jointext function to make individual powershell commands like:

    copy-item "\\source\folder$\folders\104507\104507_15A\" -destination "\\destination\folder$\folders\104507\" -recurse -force

    When I executed all the lines in sequence, I got the desired result.

    An admin can close this case, thanks to everybody who helped,

    Greetings, Djurre

     

You must be logged in to reply to this topic.