locked
Can I Exclude A Single Folder Using Copy-Item? RRS feed

  • Question

  • $s = "$Workstation\C$\Program Files (x86)\Mincom\*"
    $d = "$Workstation\C$\Program Files\Mincom"
    $files = Get-ChildItem $s -Recurse
    $i = 0
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null;
    foreach($file in $files)
    {
    $i++
    $status = "Copy file {0} on {1}:{2}..." -f $i, $files.Count, $file.Name 
    Write-Progress -Activity "Copying files to x64 location..." $status -PercentComplete (($i / $files.Count)*100)
    $h = $s.Replace("*","")
    $dpath = $file.fullname.replace($h,"")
    Copy-Item -Path $file.FullName -Destination $($d+"\"+$dpath) -exclude "*\Help*" -Force
    }
    As per the Copy-Item Cmdlet, I am trying to exclude a single folder from this copying process, which is the Help folder (as it includes 20,000~ files that I don't require being moved. I remember researching and tweaking it, separate from the script it's a part of, to where it actually worked once... aaaannnd I lost it somewhere. Any suggestions on how to exclude this Help folder from the recursive copy?
    Tuesday, June 28, 2016 3:28 AM

Answers

  • Same think in RoboCopy:

    robocopy d:\test x:\ * /XD d:\test\help /E /L

    /XD is a folder path or list of folder paths.

    /XF is a file or list of files.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 28, 2016 10:23 PM
    • Proposed as answer by Hello_2018 Friday, July 1, 2016 2:45 AM
    • Marked as answer by Hello_2018 Monday, July 4, 2016 8:18 AM
    Tuesday, June 28, 2016 10:22 PM

All replies

  • $files = Get-ChildItem$s -Recurse -Exclude '*help*'

    \_(ツ)_/

    Tuesday, June 28, 2016 3:47 AM
  • Hi jbear_alpha,

    Copy-Item PowerShell Cmdlet -Exclude parameter filter anything with "*Help*" will basically exclude filenames or folders that contains the word "*Help*".

    If you have a folder as below:
    Parent Folder: C:\Temp
    Child Folder 1: C:\Temp\Script
    Child Folder 2: C:\Temp\Help Document
    Child Folder 3: C:\Temp\Get-Help Script
    Child File 1: C:\Temp\Readme.txt
    Child File 2: C:\Temp\Help.txt
    Child File 3: C:\Temp\Get-Help.psm1

    Child Folder 2 and 3, Child File 2 and 3, will be excluded from copying because it matches wildcard help wildcard "*help*".

    It is a known behaviour discussed in this Forum post here before. Alternatively, you can try use Robocopy which handles alot better.

    Robocopy Example:

    robocopy $s $d /xd Help /copyall /tee

    Hope it helps...



    Remember to Vote as helpful for others and accept the the proposed Answer if it is relevant to build KB in this Forum. [MCITP] | [MCITP:EA] | [MCITP:VA] | [MCITP:EMA] | [MCITP:SPA] | [MCSE] | [MCSE:M+] | [MCSA] | [MCSA:M+] | [ITILv3] | [CCNA] | [IPSWITCH:WhatsUpGold]

    Tuesday, June 28, 2016 4:09 AM
  • I'll give this one a try tomorrow when I get back to the office.
    Tuesday, June 28, 2016 4:40 AM
  • You're explaining the way it is suppose to work but, it doesn't in this case. Test my code and you will see that it doesn't exclude anything even with the -Exclude parameter. I've tried both solutions mentioned above. It continues to grab all 20,000 files instead of the minimal 200 that are actually needed.
    Tuesday, June 28, 2016 9:45 PM
  • You're explaining the way it is suppose to work but, it doesn't in this case. Test my code and you will see that it doesn't exclude anything even with the -Exclude parameter. I've tried both solutions mentioned above. It continues to grab all 20,000 files instead of the minimal 200 that are actually needed.
    If you do not show us what you tried we cannot be of much help.

    \_(ツ)_/

    Tuesday, June 28, 2016 9:55 PM
  • Check the OP, I've also tried these two:

    $s = "C:\Program Files (x86)\Mincom\*"
    $d = "C:\Program Files\Mincom"
    $exclude = "*Help*"
    $files = Get-ChildItem $s -Exclude $exclude -Recurse
    $i = 0
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null;
    foreach($file in $files)
    {
    $i++
    $status = "Copy file {0} on {1}:{2}..." -f $i, $files.Count, $file.Name 
    Write-Progress -Activity "Copying files to x64 location..." $status -PercentComplete (($i / $files.Count)*100)
    $h = $s.Replace("*","")
    $dpath = $file.fullname.replace($h,"")
    Copy-Item -Path $file.FullName -Destination $($d+"\"+$dpath) -Force
    }
    
    And...

    $s = "C:\Program Files (x86)\Mincom\*"
    $d = "C:\Program Files\Mincom"
    $exclude = "*Help*"
    $files = Get-ChildItem $s -Exclude $exclude -Recurse
    $i = 0
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null;
    foreach($file in $files)
    {
    $i++
    $status = "Copy file {0} on {1}:{2}..." -f $i, $files.Count, $file.Name 
    Write-Progress -Activity "Copying files to x64 location..." $status -PercentComplete (($i / $files.Count)*100)
    $h = $s.Replace("*","")
    $dpath = $file.fullname.replace($h,"")
    robocopy $s $d /xd Help /copyall /tee
    #Copy-Item -Path $file.FullName -Destination $($d+"\"+$dpath)  -Exclude $exclude -Force
    }

    Both give the same results as the OP.
    Tuesday, June 28, 2016 10:00 PM
  • I just ran this against a folder full of giles and folders with help in the name.  It filtered all of them out:

     Get-ChildItem 'C:\Program Files\' -Exclude '*help*' -Recurse |select -expand name|?{$_ -match 'help'}


    \_(ツ)_/

    Tuesday, June 28, 2016 10:13 PM
  • This technique should work for most versions of PowerShell:

    Get-ChildItem d:\test -Directory -Recurse|?{$_.FullName -ne 'd:\test\help'}|Get-ChildItem


    \_(ツ)_/


    • Edited by jrv Tuesday, June 28, 2016 10:24 PM
    Tuesday, June 28, 2016 10:20 PM
  • Same think in RoboCopy:

    robocopy d:\test x:\ * /XD d:\test\help /E /L

    /XD is a folder path or list of folder paths.

    /XF is a file or list of files.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 28, 2016 10:23 PM
    • Proposed as answer by Hello_2018 Friday, July 1, 2016 2:45 AM
    • Marked as answer by Hello_2018 Monday, July 4, 2016 8:18 AM
    Tuesday, June 28, 2016 10:22 PM
  • I think this is the easiest way, so you can exclude a specific folder and its contents, rather than anything with the word help in it.

    $files = Get-ChildItem $s -Recurse |where {$_.FullName -notlike "$Workstation\C$\Program Files (x86)\Mincom\help\*" }

    if the c$ causes issues though, you might have to do something like this instead.

    $files = Get-ChildItem $s -Recurse |where {$_.FullName -notlike "*\Program Files (x86)\Mincom\help\*" }

    Wednesday, June 29, 2016 10:03 AM