Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Brackets in the pipeline

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-06-2007   #1 (permalink)
Dmitry Sotnikov
Guest


 

Brackets in the pipeline

It seems that brackets have some kind of inconsistent behavior when used in
the pipeline. For example if we try to append all filename names in a
directory and use pipeline from Get-ChildItem to Rename-Item round brackets
don't work. When I try the command below $_.Name just gets killed from the
expression with no error messages or anything:

dir |Rename-Item -NewName ($_.Name + "1") #This _renames_ to 1

On the other hand this works fine:

dir |Rename-Item -NewName {$_.Name + "1"} #This appends 1

However, if I use foreach instead of pipeline round brackets are OK again:

foreach ($f in dir) {Rename-Item $f -NewName ($f.Name + "1")} #This appends 1

Any insight?

--
Dmitry Sotnikov
http://dmitrysotnikov.wordpress.com

My System SpecsSystem Spec
Old 09-06-2007   #2 (permalink)
Roman Kuzmin
Guest


 

Re: Brackets in the pipeline

> dir |Rename-Item -NewName ($_.Name + "1") #This _renames_ to 1
$_.Name is evaluated once at the moment of invocation of Rename-Item (it is
invoked once), and $_ is not defined at this moment (or has no property
Name), so $_.Name is evaluated as $null and -NewName becomes '1' for any
object in the pipeline. It looks correct to me.
Quote:

> foreach ($f in dir) {Rename-Item $f -NewName ($f.Name + "1")} #This
> appends 1
This case seems clear and correct, too: $f is defined for any call of
Rename-Item which is called as many times as @(dir).Count
Quote:

> dir |Rename-Item -NewName {$_.Name + "1"} #This appends 1
First of all, this way is not documented: man Rename-Item -d: ... -newName
<string> Specifies the new name of the item ... (<string> only!)
But it works... That means Rename-Item invokes this script block for any
object in the pipeline which is presumably assigned to $_ variable.

--
Thanks,
Roman Kuzmin
PowerShellFar and FarNET: http://code.google.com/p/farnet/


"Dmitry Sotnikov" <DSotnikovREMOVETHIS@xxxxxx> wrote in message
news:8FB5ED87-944A-4E53-922D-8789DF9C68F1@xxxxxx
Quote:

> It seems that brackets have some kind of inconsistent behavior when used
> in
> the pipeline. For example if we try to append all filename names in a
> directory and use pipeline from Get-ChildItem to Rename-Item round
> brackets
> don't work. When I try the command below $_.Name just gets killed from the
> expression with no error messages or anything:
>
> dir |Rename-Item -NewName ($_.Name + "1") #This _renames_ to 1
>
> On the other hand this works fine:
>
> dir |Rename-Item -NewName {$_.Name + "1"} #This appends 1
>
> However, if I use foreach instead of pipeline round brackets are OK again:
>
> foreach ($f in dir) {Rename-Item $f -NewName ($f.Name + "1")} #This
> appends 1
>
> Any insight?
>
> --
> Dmitry Sotnikov
> http://dmitrysotnikov.wordpress.com

My System SpecsSystem Spec
Old 09-06-2007   #3 (permalink)
Edengundam
Guest


 

Re: Brackets in the pipeline

"Dmitry Sotnikov" <DSotnikovREMOVETHIS@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:8FB5ED87-944A-4E53-922D-8789DF9C68F1@xxxxxx
Quote:

> It seems that brackets have some kind of inconsistent behavior when used
> in
> the pipeline. For example if we try to append all filename names in a
> directory and use pipeline from Get-ChildItem to Rename-Item round
> brackets
> don't work. When I try the command below $_.Name just gets killed from the
> expression with no error messages or anything:
>
> dir |Rename-Item -NewName ($_.Name + "1") #This _renames_ to 1
This command is parsed under command mode. The ($_.Name + "1") is the same
as $null.name + "1" before the command starts. In other words, It just like
: dir |Rename-Item -NewName ("" + "1")
Quote:

>
> On the other hand this works fine:
>
> dir |Rename-Item -NewName {$_.Name + "1"} #This appends 1
The {} means making it as an scriptblock, so this piece of code would be
evaluated every time when an object is piped into. As result, the $_ refers
the object which is piped by 'dir'.
Quote:

>
> However, if I use foreach instead of pipeline round brackets are OK again:
>
> foreach ($f in dir) {Rename-Item $f -NewName ($f.Name + "1")} #This
> appends 1
The output of pipeline in foreach statement are collected firstly. So $f
refers to the entry in dir properly.
Quote:

>
> Any insight?
>
> --
> Dmitry Sotnikov
> http://dmitrysotnikov.wordpress.com
get-help about_parsing may help you.

Tao Ma (Edengundam)
Best wishes!


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to rename files with brackets in the filename Yorick PowerShell 8 07-04-2007 07:50 AM
square brackets, updated version, worse than before Whisperer PowerShell 0 03-29-2007 10:54 AM
square brackets, filenames, get-acl, get-item:solved (almost) Whisperer PowerShell 4 03-29-2007 10:48 AM
How to CD into a folder with [square brackets] in its name? Adahn PowerShell 5 06-02-2006 04:37 PM
File/Folder names with [square brackets] Adahn Vista file management 0 05-12-2006 07:03 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51