![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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.
Rename-Item which is called as many times as @(dir).Count
<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
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: Brackets in the pipeline "Dmitry Sotnikov" <DSotnikovREMOVETHIS@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:8FB5ED87-944A-4E53-922D-8789DF9C68F1@xxxxxx
as $null.name + "1" before the command starts. In other words, It just like : dir |Rename-Item -NewName ("" + "1")
evaluated every time when an object is piped into. As result, the $_ refers the object which is piped by 'dir'.
refers to the entry in dir properly.
Tao Ma (Edengundam) Best wishes! | ||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
| 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 |