none
Copy-Item in an Array while keeping folder structure

    Question

  • Write-Host "Data Count" -foregroundcolor Green
    Write-Host "=========================" -foregroundcolor Green
    $drive = "C"
    Write-Host "Gathering Data Count" -foregroundcolor magenta
    # Lists in an array all directories and files that need to be counted
    $directory = get-childitem $drive`:\* -force -exclude *windows*, *Intel*, '*Program Files*', *.ini*, *Users*, *PerfLogs*, *eula*, *.dll*, *VC*, *.sys*, *Recycle*, '*Documents and Settings*', *ProgramData*, *Recovery*, '*System Volume Information*'
    $directory += get-childitem $drive`:\Users\* -exclude '*All Users*', *Default*, '*Default User*', *AppData*, '*Application Data*', *Cookies*, *.dll*, *.ini*, *Microsoft*, *NetHood*, *PrintHood*, *Recent*, *SendTo*, '*Start Menu*', *Templates*, '*Saved Games*', *Searches* 
    
    foreach ($dir in $directory) {
        $a = get-childitem $dir -recurse | foreach-object -process { $_.FullName }
        copy-item $a "Z:\backups\Test\" -force -verbose
    }
    For some reason I can't get the folder structure to stay.  Any help?
    Saturday, July 27, 2013 6:10 PM

All replies

  • Write-Host "Data Count" -foregroundcolor Green
    Write-Host "=========================" -foregroundcolor Green
    $drive = "C"
    Write-Host "Gathering Data Count" -foregroundcolor magenta
    # Lists in an array all directories and files that need to be counted
    $directory = get-childitem $drive`:\* -force -exclude *windows*, *Intel*, '*Program Files*', *.ini*, *Users*, *PerfLogs*, *eula*, *.dll*, *VC*, *.sys*, *Recycle*, '*Documents and Settings*', *ProgramData*, *Recovery*, '*System Volume Information*'
    $directory += get-childitem $drive`:\Users\* -exclude '*All Users*', *Default*, '*Default User*', *AppData*, '*Application Data*', *Cookies*, *.dll*, *.ini*, *Microsoft*, *NetHood*, *PrintHood*, *Recent*, *SendTo*, '*Start Menu*', *Templates*, '*Saved Games*', *Searches* 
    
    foreach ($dir in $directory) {
        $a = get-childitem $dir -recurse | foreach-object -process { $_.FullName } | sort FullName
        copy-item $a "Z:\backups\Test\" -force -verbose
    }
    Added "Sort FullName", does this help?
    Saturday, July 27, 2013 6:39 PM
  • I you want to copy the whole directory structure, you really should think about using Robocopy instead. Robocopy has been designed for exactly that purpose and might give you a better and safer alternative (here you get one example: http://stackoverflow.com/questions/15562443/using-powershell-to-copy-entire-directory-structure).

    wizend

    Saturday, July 27, 2013 7:49 PM
  • Ok, I started using robocopy and I still can't get a folder structure.  Here is the new code:

    Write-Host "Data Count" -foregroundcolor Green
    Write-Host "=========================" -foregroundcolor Green
    $drive = "C"
    Write-Host "Gathering Data Count" -foregroundcolor magenta
    # Lists in an array all directories and files that need to be counted
    $directory = get-childitem $drive`:\* -force -exclude *windows*, *Intel*, '*Program Files*', *.ini*, *Users*, *PerfLogs*, *eula*, *.dll*, *VC*, *.sys*, *Recycle*, '*Documents and Settings*', *ProgramData*, *Recovery*, '*System Volume Information*'
    $directory += get-childitem $drive`:\Users\* -exclude '*All Users*', *Default*, '*Default User*', *AppData*, '*Application Data*', *Cookies*, *.dll*, *.ini*, *Microsoft*, *NetHood*, *PrintHood*, *Recent*, *SendTo*, '*Start Menu*', *Templates*, '*Saved Games*', *Searches* 
    
    foreach ($dir in $directory) {
        #$a = get-childitem $dir -recurse | foreach-object -process { $_.FullName }
        robocopy "$dir" Z:\Backups\Test /MIR
    }
    I have also used /s with no luck.

    Monday, July 29, 2013 3:17 PM
  • Hi,

    How about:

    robocopy "$dir"  z:\backup\test /mir /zb /sec

    Or

    robocopy $dir z:\backup\test /s

    Regards,

    Yan Li


    Cataleya Li
    TechNet Community Support

    Tuesday, July 30, 2013 5:46 AM
    Moderator
  • That foreach loop does more harm than good. Robocopy can copy your complete folder structure without much ado and thereby ensures that those directories you wish to leave out are not involved. Here is a short example for a batch script to copy a certain folder structure.

    Set Source="c:"
    Set Destination="Z:\backups\Test"
    Set LogFile=%wiz%\logf.log
    
    robocopy  %Source% %Destination% *.* /E /XD *windows* *Intel* "*Program Files*" *Users* *PerfLogs* *eula* *VC* *Recycle* "*Documents and 
    
    Settings*" *ProgramData* *Recovery* "*System Volume Information*" /xf *.ini *.dll* *.sys* /log:%Logfile%
    

    Use the '/XD' option to exclude directories and the '/XF' option to exclude files. And leave out any trailing slashes on source and destination directory names, otherwise robocopy doesn't work correctly. The '/log:<logfile>' option does what you'ld expect it to do.

    Better to check it first in a small test environment to get accustomed with the syntax.

    wizend

    • Proposed as answer by Mike Laughlin Tuesday, July 30, 2013 1:35 PM
    Tuesday, July 30, 2013 9:11 AM
  • @wizend

    That sort of worked.  I get a folder structure now but instead of this:

    C:\users\owners\desktop -> Z:\backups\test\users\owners\desktop

    I get:

    C:\users\owners\desktop -> Z:\backups\test\desktop

    It's skipping folders in the structure and that is just running the following:

    robocopy  "C:" "Z:\backups\Test" *.* /E /XD *windows* *Intel* "*Program Files*" *Users* *PerfLogs* *eula* *VC* *Recycle* "*Documents and Settings*" *ProgramData* *Recovery* "*System Volume Information*" /xf *.ini *.dll* *.sys* *.dat* /log:%Logfile%
    Here is part if the log
    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows                              
    -------------------------------------------------------------------------------
    
      Started : Tue Jul 30 09:03:53 2013
    
       Source : C:\Users\Owner\
         Dest : Z:\backups\Test\
    
        Files : *.*
    	    
    Exc Files : *.ini
    	    *.dll*
    	    *.sys*
    	    *.dat*
    	    
     Exc Dirs : *windows*
    	    *Intel*
    	    *Program Files*
    	    *Users*
    	    *PerfLogs*
    	    *eula*
    	    *VC*
    	    *Recycle*
    	    *Documents and Settings*
    	    *ProgramData*
    	    *Recovery*
    	    *System Volume Information*
    	    
      Options : *.* /S /E /COPY:DAT /R:1000000 /W:30 
    
    ------------------------------------------------------------------------------
    
    	                  27	C:\Users\Owner\
    	    Newer     		       0	%Logfile%
      0%  
    100%  
    	    New File  		  13.2 m	PCPE Setup.exe
      0.0%
      7.5%
     15.0%
     22.5%
     30.1%
     37.6%
     45.1%
     52.7%
     60.2%
     67.7%
     75.3%
     82.8%
     90.3%
     97.9%
    100%  
    	    New File  		  12.7 m	PCPE_3.0.1.msi
      0.0%
      7.8%
     15.7%
     23.5%
     31.4%
     39.3%
     47.1%
     55.0%
     62.8%
     70.7%
     78.6%
     86.4%
     94.3%
    100%  
    	    New File  		      30	textmate_rspec_last_remembered_file_cache.txt
      0%  
    100%  
    	  New Dir          0	C:\Users\Owner\.eclipse\
    	  New Dir          1	C:\Users\Owner\.eclipse\org.eclipse.equinox.security\
    	    New File  		    1740	secure_storage
      0%  
    100%  
    	  New Dir          7	C:\Users\Owner\.VirtualBox\
    	    New File  		    1324	selectorwindow.log
      0%  
    100%  
    	    New File  		     862	selectorwindow.log.1
      0%  
    100%  
    	    New File  		    3621	VBoxSVC.log
      0%  
    100%  
    	    New File  		    1767	VBoxSVC.log.1
      0%  
    100%  
    	    New File  		     880	VBoxSVC.log.2
      0%  
    100%  
    	    New File  		    2238	VirtualBox.xml
      0%  
    100%  
    	    New File  		    2139	VirtualBox.xml-prev
      0%  
    100%  
    	  New Dir          0	C:\Users\Owner\AppData\
    	  New Dir          3	C:\Users\Owner\AppData\Local\


    • Edited by Joseph Monarch Tuesday, July 30, 2013 2:13 PM Left out log
    Tuesday, July 30, 2013 2:10 PM
  • I forgot those junction points in OS > XP (for details, see here). Those OS have certaiin special folders that work like redirection points to other directories. I think that might have caused that problem. You can avoid it by adding another option '/XJ' to your command:

    robocopy  "C:" "Z:\backups\Test" *.* /E /XJ /XD *windows* *Intel* "*Program Files*" *Users* *PerfLogs* *eula* *VC* *Recycle* "*Documents and Settings*" *ProgramData* *Recovery* "*System Volume Information*" /xf *.ini *.dll* *.sys* *.dat* /log:%Logfile%


    Tuesday, July 30, 2013 2:57 PM
  • It's still skipping the same folders in the structure.  So frustrating. 
    Wednesday, July 31, 2013 2:39 PM