locked
How to exclude a sub folder and its contents using get-childitem RRS feed

  • Question

  • Hi,

    I have a folder which contain both sub folders and files. I want to copy all the contents (Files,subfolders and its contents) excluding a specific subfolder and its contents. I tried the below one but it didn't work.

    $inputFolder = 'D:\dinesh\run'
    $excludefolder= 'dataset'
    $target = 'E:\kumar\run'
    Get-ChildItem -path $inputFolder -Recurse | Where {$_.FullName -notlike $excludefolder} | Copy-Item -Destination {Join-Path $target $_.FullName.Substring($inputFolder.length)}

    The above command is copying the $excludefolder contents as well but it should not.
    Can any one suggest a way to get it done.

    Thanks.
    Monday, June 18, 2018 4:52 PM

Answers

  • Where {$_.DirectoryName -notmatch $excludefolder}

    \_(ツ)_/

    • Marked as answer by Dinesh Edvin Tuesday, June 19, 2018 9:51 AM
    Monday, June 18, 2018 4:59 PM

All replies

  • Where {$_.DirectoryName -notmatch $excludefolder}

    \_(ツ)_/

    • Marked as answer by Dinesh Edvin Tuesday, June 19, 2018 9:51 AM
    Monday, June 18, 2018 4:59 PM
  • Just because I like to keep it simple, the following will work:

    get-childitem D:\dinesh\run\ | Where {$_.Name -ne'dataset'} | Copy-Item -Destination E:\kumar\run


    • Proposed as answer by lxDKxl Monday, June 18, 2018 6:10 PM
    • Edited by lxDKxl Monday, June 18, 2018 6:12 PM
    Monday, June 18, 2018 6:10 PM
  • Just because I like to keep it simple, the following will work:

    get-childitem D:\dinesh\run\ | Where {$_.Name -ne'dataset'} | Copy-Item -Destination E:\kumar\run


    That will only exclude the folder itself, not any files or folders underneath it.

    ls -r foo | where name -ne foo2 | select mode,name,fullname

    Mode   Name  FullName
    ----   ----  --------
    -a---- file  C:\users\me\foo\file
    d----- foo3  C:\users\me\foo\foo2\foo3
    -a---- file2 C:\users\me\foo\foo2\file2
    -a---- file3 C:\users\me\foo\foo2\foo3\file3

    Maybe something like this (using -notlike to avoid the regex):

    ls -r foo | where fullname -notlike c:\users\me\foo\foo2* | select mode,name,fullname

    Mode   Name FullName                   
    ----   ---- --------                   
    -a---- file C:\users\me\foo\file 








    • Edited by JS2010 Tuesday, June 19, 2018 5:19 PM
    Tuesday, June 19, 2018 4:40 PM