Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Brackets in the pipeline

Reply
 
Old 09-06-2007   #1 (permalink)
Dmitry Sotnikov


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
How to rename files with brackets in the filename PowerShell
square brackets, updated version, worse than before PowerShell
square brackets, filenames, get-acl, get-item:solved (almost) PowerShell
How to CD into a folder with [square brackets] in its name? PowerShell
File/Folder names with [square brackets] Vista file management


Vista Forums 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 Ltd

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