Windows Vista Forums

get-childitem -exclude bug

  1. #1


    bep Guest

    get-childitem -exclude bug

    Hello

    It seems that Powershell 1.0 was shipped without a working -exclude
    parameter on any of the file IO functions (Get-childitem, Remove-Item,
    Move-Item, Copy-Item, etc.



    Has anyone made or found replacement cmdlets that do have working -exclude
    parameters?

    This is a really strange thing. I think Powershell is one of the best things
    to come out of MS in the recent years, and have been using it since the first
    Beta. But I have not needed the esclude until now, and it surprises me that
    such fundamental functionality does not work. It further surprises me that
    the parameter was left in, and the help just says it does not work. Wy not
    ship without the parameter?

    My situation is that I need to copy from a source path to a destunation
    path, recursively, with a bunch of files and folder names excluded from the
    copy. Examples of the exclude list are ("*.cs", "*.sln","*.suo", "*.csproj",
    "*.csproj.*", "\obj")

    Thanks
    David

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: get-childitem -exclude bug

    bep@xxxxxx wrote:

    > Hello
    >
    > It seems that Powershell 1.0 was shipped without a working -exclude
    > parameter on any of the file IO functions (Get-childitem, Remove-Item,
    > Move-Item, Copy-Item, etc.
    >
    > Has anyone made or found replacement cmdlets that do have working -exclude
    > parameters?
    >
    > This is a really strange thing. I think Powershell is one of the best things
    > to come out of MS in the recent years, and have been using it since the first
    > Beta. But I have not needed the esclude until now, and it surprises me that
    > such fundamental functionality does not work. It further surprises me that
    > the parameter was left in, and the help just says it does not work. Wy not
    > ship without the parameter?
    >
    > My situation is that I need to copy from a source path to a destunation
    > path, recursively, with a bunch of files and folder names excluded from the
    > copy. Examples of the exclude list are ("*.cs", "*.sln","*.suo", "*.csproj",
    > "*.csproj.*", "\obj")
    >
    > Thanks
    > David
    Yes, I'm sure it can be done, especially if you need to create
    directories at the destination, but it may be easier for you to use
    something like xcopy from DOS.

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  3. #3


    bep Guest

    Re: get-childitem -exclude bug

    Yes, I though about that, but it seems a large step backwards. I thought
    Powershell was the future.

    The biggest problem I have with xcopy is the reliance on a file on the hard
    drive listing all of the exclude items. If I could pass in the exclusion list
    via memory, it would be acceptable. The file slows things down a little, and
    is a risk if that file is not there at runtime.

    Thanks
    David

    "Marco Shaw [MVP]" wrote:

    > bep@xxxxxx wrote:

    > > Hello
    > >
    > > It seems that Powershell 1.0 was shipped without a working -exclude
    > > parameter on any of the file IO functions (Get-childitem, Remove-Item,
    > > Move-Item, Copy-Item, etc.
    > >
    > > Has anyone made or found replacement cmdlets that do have working -exclude
    > > parameters?
    > >
    > > This is a really strange thing. I think Powershell is one of the best things
    > > to come out of MS in the recent years, and have been using it since the first
    > > Beta. But I have not needed the esclude until now, and it surprises me that
    > > such fundamental functionality does not work. It further surprises me that
    > > the parameter was left in, and the help just says it does not work. Wy not
    > > ship without the parameter?
    > >
    > > My situation is that I need to copy from a source path to a destunation
    > > path, recursively, with a bunch of files and folder names excluded from the
    > > copy. Examples of the exclude list are ("*.cs", "*.sln","*.suo", "*.csproj",
    > > "*.csproj.*", "\obj")
    > >
    > > Thanks
    > > David
    >
    > Yes, I'm sure it can be done, especially if you need to create
    > directories at the destination, but it may be easier for you to use
    > something like xcopy from DOS.
    >
    > Marco
    >
    > --
    > Microsoft MVP - Windows PowerShell
    > http://www.microsoft.com/mvp
    >
    > PowerGadgets MVP
    > http://www.powergadgets.com/mvp
    >
    > Blog:
    > http://marcoshaw.blogspot.com
    >

      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: get-childitem -exclude bug

    Don't give up on PowerShell. This quick script should give you an idea, it'll copy all files -except for those excluded- from <Source> to <Destination> maintaining Source's tree structure. It overwrites any existing item, but you can easily modify that or any other inconvinience. I tested it on v2 CTP, should work on v1.

    Pass the full path to Source, Destination and the contents of the exclusions file in an expression:

    .\Copy-Item_2 <source> <destination> (gc <exclusions file>)

    -< Copy-Item_2.ps1 >-
    param ([string]$src, [string]$dest, [string[]]$exc)

    # create an escaped version of Source to replace it with Destination
    $escSrc = [regex]::escape($src)

    # get all items not excluded
    $items = ls $src -ex $exc -r -fo

    # create subdirs in Destination
    $items | ? {$_.psIsContainer} | sort fullname |
    % {[void](ni ($_.fullname -replace $escSrc, $dest) -i d* -ea 0)}

    # copy files to their respective subdir
    $items | ? {!$_.psIsContainer} |
    % {cpi $_.fullname ($_.fullname -replace $escSrc, $dest) -fo -ea 0}
    -< end >-

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Maximilian Hänel Guest

    Re: get-childitem -exclude bug

    Hi

    > The biggest problem I have with xcopy is the reliance on a file on the hard
    > drive listing all of the exclude items. If I could pass in the exclusion list
    > via memory, it would be acceptable. The file slows things down a little, and
    > is a risk if that file is not there at runtime.
    And what about robocopy? That's my personal favorite...

    cu

    Max

      My System SpecsSystem Spec

  6. #6


    Marco Shaw [MVP] Guest

    Re: get-childitem -exclude bug

    bep@xxxxxx wrote:

    > Yes, I though about that, but it seems a large step backwards. I thought
    > Powershell was the future.
    Kiron provided a good example.

    PowerShell is the future, but it still has some things that can be improved.

    The ability to drop back to good old DOS commands isn't all that bad
    sometimes, and helps with older stuff.

    Marco

      My System SpecsSystem Spec

  7. #7


    Gordon Bell Guest

    Re: get-childitem -exclude bug

    David,

    Get-ChildItem works if all you want to do is exclude file extensions and
    use the syntax:

    get-childitem C:\Folder -recurse -exclude *.cs,*.sln,*.suo,*.csproj.*

    But I've been unable to get the other commands to work properly with
    exclude. Also, to exclude subfolders like \obj\, I think you'll need a
    custom function.

    Gordon


    bep@xxxxxx wrote:

    > Hello
    >
    > It seems that Powershell 1.0 was shipped without a working -exclude
    > parameter on any of the file IO functions (Get-childitem, Remove-Item,
    > Move-Item, Copy-Item, etc.
    >
    > Has anyone made or found replacement cmdlets that do have working -exclude
    > parameters?
    >
    > This is a really strange thing. I think Powershell is one of the best things
    > to come out of MS in the recent years, and have been using it since the first
    > Beta. But I have not needed the esclude until now, and it surprises me that
    > such fundamental functionality does not work. It further surprises me that
    > the parameter was left in, and the help just says it does not work. Wy not
    > ship without the parameter?
    >
    > My situation is that I need to copy from a source path to a destunation
    > path, recursively, with a bunch of files and folder names excluded from the
    > copy. Examples of the exclude list are ("*.cs", "*.sln","*.suo", "*.csproj",
    > "*.csproj.*", "\obj")
    >
    > Thanks
    > David

      My System SpecsSystem Spec

  8. #8


    Kiron Guest

    Re: get-childitem -exclude bug

    I missed the folder names in the exclusions, the 'quick' script does not exclude subDirs under the excluded folder that contain non excluded files. Should work as long as the excluded folder names begin with a backslash '\' and don't contain an asterisk '*'.

    I've attached two modified versions that will:

    Script1 when exclusions are stored as one string, like:
    "*.cs","*.sln","*.suo","*.csproj","*.csproj.*","\obj"

    Script2 when exclusions are stored as a single line, like:
    *.cs
    *.sln
    *.suo
    *.csproj
    *.csproj.*
    \obj


    --
    Kiron

      My System SpecsSystem Spec

get-childitem -exclude bug

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exclude directories with Get-ChildItem Jeff PowerShell 6 11 Feb 2008
Bug in Get-ChildItem -exclude loworbit@gmail.com PowerShell 4 19 Dec 2006
RE: Exclude directories from Get-ChildItem? dreeschkind PowerShell 0 29 Nov 2006
RE: Exclude directories from Get-ChildItem? RichS PowerShell 0 29 Nov 2006
Exclude directories from Get-ChildItem? ~Clint PowerShell 0 29 Nov 2006