none
copy a folder except a subfolder recursively confused me RRS feed

  • Question

  • Hi Scripting Guys!
    When i try to copy a  folder  except a subfolder  recursively I found something what confused me very much 
    In my test environment i have a parent "E:\Test" , four subfolder named 1 、 2 、3 、4 and two .txt file name 5.txt 6.txt
    folder 1 is empty ,folder 2 have a empty subfolder name 2.1 a .txt file name 2.2
    folder 3 have a empty subfolder name 3.1 a .txt file name 3.2  
    folder 4 have a empty subfolder name 4.1

    I want to copy "E:\Test" to F:\ except subfolder “3” recursively 
    so i use the command below :

    but the test Before and after rename puzzled me how can i copy "E:\Test" to F:\ except subfolder “3” recursively 

    Remove-Item -Path "F:\Test" -Force -Recurse -ea 0
    Get-ChildItem -Path  "E:\Test" -Exclude "3" | % {Copy-Item $_ "F:\Test"  -Container -Recurse -Force -Verbose}



    
    Saturday, May 30, 2020 12:46 PM

Answers

  • The easiest way to do this is to use RoboCopy which can duplicte a structure and slip and files or folders you wish.

    robocopy /?


    \_(ツ)_/

    • Marked as answer by badacook Thursday, June 25, 2020 12:52 AM
    Tuesday, June 2, 2020 1:18 AM

All replies

  • Either you omit the parameter -Container in your example:
    Get-ChildItem -Path  E:\Test -Exclude 3 |
        ForEach-Object { 
            Copy-Item -Path $_ -Destination F:\Test -Recurse -Force -Verbose 
        }

    Or - while Copy-Item have a parameter -Exclude as well - you make it even simpler like this:
    Copy-Item -Path E:\Test\* -Destination F:\Test\ -Exclude 3 -Recurse -Force -Verbose


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Saturday, May 30, 2020 11:01 PM
    Saturday, May 30, 2020 11:00 PM
  • First of all thanks for your reply 

    But i am so disappointed that you even haven't a test before 

    Sunday, May 31, 2020 3:05 AM
  • But i am so disappointed that you even haven't a test before 

    I don't get it. What does that mean? I tested it and it's working for me just as expected


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sunday, May 31, 2020 11:09 AM
  • I mean that as the second image i had pasted 
    The directory structure  of  "F:\Test" have been changed
    how can i copy a folder except subfolder 3 recursively  and keeping the orgin directory structure   
    Tuesday, June 2, 2020 12:40 AM
  • I posted the answer!! I posted two options you can choose from.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, June 2, 2020 12:55 AM
  • The easiest way to do this is to use RoboCopy which can duplicte a structure and slip and files or folders you wish.

    robocopy /?


    \_(ツ)_/

    • Marked as answer by badacook Thursday, June 25, 2020 12:52 AM
    Tuesday, June 2, 2020 1:18 AM
  • Hi jrv

      Long time no see seem my last question

    it's really thanks  for your help And Happy Dragon Boat Festival! 

        
    Thursday, June 25, 2020 1:03 AM
  • May the fiercest dragon win and all have a good festival.  I thought that was on June 7?


    \_(ツ)_/

    Thursday, June 25, 2020 1:06 AM