locked
Powershell copy folders and files and re-create parent folder as sub folder RRS feed

  • Question

  • I created simple powershell script to copy files and folders from one location to many location in share computers, this work well but we have to run this script one time every week to copy and over write existen content

    The problem is the script next time run create the parents folder inside same parent agian as sub folder for exampel:

    Folder1 copied (Next time script run or if the folder exist) this look like

    Folder1\folder1


    this copy correctly the folder and content but if the folder exist or this will create new sub folder in the same folder with the same name

    Here is copy of the script to be reviewd.

    Clear-Host
    #source path
    $sourceDir = '\\source\Users\asdew\Desktop\dep' 
    
    
    #Array with Sites Path
    $targetDir = @(
    
    
                   #Sites en la maquina1
                   '\\Apc20\Users\afded\Desktop\site01',
     
    
                   #Sites en la maquina2 
    
                   '\\Apc10\Users\adsed\Desktop\site02'
    
                  
    
    
    
                    )
    
    $i=0
    
    
    
    Foreach ($dest in  $targetDir){
    
    Get-ChildItem $sourceDir   -Recurse   | % {
    
       $dest = $targetDir[$i] + $_.FullName.SubString($sourceDir.Length)
       
        If (!($dest.Contains('.')) -and !(Test-Path $dest))
    
        {
            mkdir $dest
        }
     
      
    
           #Copy Files and over write existen files.
       Write-Host "copy  start. Please wait..We are copying your files.." -ForegroundColor DarkYellow
    
       Copy-Item $_.FullName -Destination $dest -Force  -PassThru -ErrorAction SilentlyContinue
    
       If(-not $?)
        
        {Write-Warning "Your copy Failed!!"  -InformationAction} 
    
       else 
        {Write-Host "Your copy was Success!" -ForegroundColor Green}
       
          
       }
    
    
        
        $i =  $i+1
    }

    I hope some one can help me to resolve this issue.

    Regards

    Wednesday, August 17, 2016 12:14 PM

Answers

  • Not if you do it my way or use RoboCopy.

    \_(ツ)_/

    • Proposed as answer by Hello_2018 Monday, August 29, 2016 7:22 AM
    • Marked as answer by Hello_2018 Tuesday, September 6, 2016 3:22 AM
    Wednesday, August 17, 2016 5:10 PM
  • Hi  tecnico112,

    >>Powershell copy folders and files and re-create parent folder as sub folder

    I have used the following testing example :

    Copy-Item .\folder1 -Destination \\nlb\folder  -Force

    No parent folder exists in subforder.

    Besides, I have tried to use the following script to copy, for you reference:

    function do-something
    {
        #the following command used to load visual basic
        [void][system.reflection.assembly]::LoadWithPartialName('microsoft.visualbasic')
        [string]$path = [Microsoft.VisualBasic.Interaction]::inputbox('Pls enter file path','File Path','c:\1.txt')
        [string]$destination = [Microsoft.VisualBasic.Interaction]::InputBox('Pls enter destination location-ip address/upn address ','Destination Path','\\10.168.196.93\powershell-scripts')
    
        try{
            Copy-Item -Path $path -Destination $destination -ErrorAction Stop
            [Microsoft.VisualBasic.Interaction]::MsgBox('Copy to destination complete!', "OKOnly,SystemModal,Information", "Success")
    
        }catch{
            $_.exception.message
        }
    }
    do-something

    Best regards,

    Andy_Pan


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft..com.

    • Proposed as answer by Hello_2018 Monday, August 29, 2016 7:22 AM
    • Marked as answer by Hello_2018 Tuesday, September 6, 2016 3:22 AM
    Thursday, August 18, 2016 8:44 AM

All replies

  • This would best be done with RoboCopy as RC cn recreate  folder structure and it is much faster than any other method.

    \_(ツ)_/

    Wednesday, August 17, 2016 12:33 PM
  • For a simple copy of a folder hierarchy from source to destination

    copy-item $sourceDir $targetDir -Recurse -force


    \_(ツ)_/

    Wednesday, August 17, 2016 12:38 PM
  • I will try it with Robocopy.Thanks in advanced.
    Wednesday, August 17, 2016 12:57 PM
  • I´m using  copy-item but without -Recurse,

    The problem is when I run the script for second time to update the files

    Then the script re-create the folders agian as sub folders Example

    Folder named F01 next time I run the script F01 Will be updated and insde the F01 Will be created a new empty folder named also F01. this the problem. even using -Recurse.

    I Hope that I explained well.

    Thanks for your help.

    Wednesday, August 17, 2016 1:00 PM
  • Not if you do it my way or use RoboCopy.

    \_(ツ)_/

    • Proposed as answer by Hello_2018 Monday, August 29, 2016 7:22 AM
    • Marked as answer by Hello_2018 Tuesday, September 6, 2016 3:22 AM
    Wednesday, August 17, 2016 5:10 PM
  • Hi  tecnico112,

    >>Powershell copy folders and files and re-create parent folder as sub folder

    I have used the following testing example :

    Copy-Item .\folder1 -Destination \\nlb\folder  -Force

    No parent folder exists in subforder.

    Besides, I have tried to use the following script to copy, for you reference:

    function do-something
    {
        #the following command used to load visual basic
        [void][system.reflection.assembly]::LoadWithPartialName('microsoft.visualbasic')
        [string]$path = [Microsoft.VisualBasic.Interaction]::inputbox('Pls enter file path','File Path','c:\1.txt')
        [string]$destination = [Microsoft.VisualBasic.Interaction]::InputBox('Pls enter destination location-ip address/upn address ','Destination Path','\\10.168.196.93\powershell-scripts')
    
        try{
            Copy-Item -Path $path -Destination $destination -ErrorAction Stop
            [Microsoft.VisualBasic.Interaction]::MsgBox('Copy to destination complete!', "OKOnly,SystemModal,Information", "Success")
    
        }catch{
            $_.exception.message
        }
    }
    do-something

    Best regards,

    Andy_Pan


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft..com.

    • Proposed as answer by Hello_2018 Monday, August 29, 2016 7:22 AM
    • Marked as answer by Hello_2018 Tuesday, September 6, 2016 3:22 AM
    Thursday, August 18, 2016 8:44 AM