PowerShell scripting | Copy folders from Source to Destination issue

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of lpk . lpk . 3 months, 2 weeks ago.

  • Author
    Posts
  • #48410
    Profile photo of lpk .
    lpk .
    Participant
    Hi, 
    Can anyone be help on the below query:-
    
    I have a requirement in Powershell to exclude certain files and folders while copying em into destination.
    
    Source structure(Original):
    -----------------------------------
    Rootfolder>Subfolder1>Subfolder1_1>demo.txt
    Rootfolder>Subfolder2
    Rootfolder>Subfolder3
    Rootfolder>sample.txt
    
    Script:
    --------
    $ExcludeFolders = @("subfolder2")
    Get-ChildItem -Path $FROM -Exclude $ExcludeFolders |
    Copy-Item -Destination $TO -force -recurse
    
    When the above script is run, the result is given below-
    
    Destination structure (Not expected):
    -----------------------------------------------
    Rootfolder>Subfolder1>Subfolder1_1>demo.txt
    Rootfolder>Subfolder3
    Rootfolder>sample.txt
    Rootfolder>demo.txt     
    
    But, Destination structure (Expected) = Source structure(Original)
    
    I am able to exclude the required folders and files while performing the copy-item. However, what is bothering me is , one of the files from 2nd level sub folder is being copied into the root folder too leading to the duplicate file in the destination file structure.
    
    In my above sample, 'demo.txt' is the file that is residing in 'Subfolder1_1' which is in 'Subfolder1' and in turn this folder is in 'Root' folder.But when the script is run, I could see the demo.txt file being copied in 'Root' folder too. (Root>demo.txt). Which means, the file 'demo.txt' is existing in its original folder (SubFolder1_1) as well as in 'Root' folder.
    
    How do I get rid of unwanted duplicate files as discussed in the above scenario?
    
  • #48420
    Profile photo of Edmond Yee
    Edmond Yee
    Participant

    It might be easier if you separated your commands out to generate your folder list first with something like:

     #Populate list of folders
    $folderlist = Get-ChildItem -Path $FROM -Exclude $ExcludeFolders -Recurse | Where {$_.PSIsContainer -eq $True}
    ForEach ($folder in $folderlist)
    {
       Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item -Destination $TO -force -recurse
    }
    • This reply was modified 4 months, 1 week ago by Profile photo of Edmond Yee Edmond Yee.
    • #48522
      Profile photo of lpk .
      lpk .
      Participant

      Hi Edmon,

      The above script is giving me the below error.

      I have modified the script according to my need. Please have a look-

      $ExcludeFolders = @("DES", "Help","LoadBalancer")
      $ExcludeFiles=@("web.config")
      $folderlist = Get-ChildItem -Path "c:\temp\" -Exclude $ExcludeFolders -Recurse | Where {$_.PSIsContainer -eq $True}

      ForEach ($folder in $folderlist)

      {

      Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item -Destination "d:\july28\" -force -recurse

      }

      The error i get is:-

      Copy-Item : The filename, directory name, or volume label syntax is incorrect.
      At line:3 char:63
      + Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item < <<< -Destination "d:\july28\" -force -recurse + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand Copy-Item : The filename, directory name, or volume label syntax is incorrect. At line:3 char:63 + Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item <<<< -Destination "d:\july28\" -force -recurse + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

  • #48941
    Profile photo of Edmond Yee
    Edmond Yee
    Participant

    I just copied and pasted your code, setup the following folders:

    C:\temp\DES
    C:\temp\Help
    C:\temp\LoadBalancer
    C:\temp\test

    and in the test folder created the files test.txt and web.config.

    Once I ran the script, it ran as expected without errors. Is the destination valid for you? Meaning does the July28 folder already exist on D:\ when you run the script or are you expecting it to create it for you as well? If so, you would use something like this:

    $date = get-date -uformat %B%d
    $destination = "E:\$date"
    If ((Test-Path $destination) -eq $False)
    {
        New-Item "$destination" -ItemType Directory
    }
  • #51067
    Profile photo of lpk .
    lpk .
    Participant

    Yeah Edmond!

    This is perfect. Thanks a ton! 🙂
    Apologize for the late response. I was out of station.

    • This reply was modified 3 months, 2 weeks ago by Profile photo of lpk . lpk ..

You must be logged in to reply to this topic.