![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Rename-Item which is called as many times as @(dir).Count Quote: > dir |Rename-Item -NewName {$_.Name + "1"} #This appends 1 <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 Specs![]() |
| | #3 (permalink) |
| | 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 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 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 refers to the entry in dir properly. Quote: Tao Ma (Edengundam) Best wishes! |
My System Specs![]() |
![]() |
| 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 | |||