powershell - Rename a list of files -


i need rename files below pwd named all_v4_0.csv all_v4_1.csv. far, have worked way piece of powershell:

$oldfiles = dir -recurse | ?{$_.name -eq "all_v4_0.csv"} foreach ($o in $oldfiles) {     $o.copyto join-path $o.directory.tostring() "all_v4_1.csv" } 

but foreach loop fails message that

at line:2 char:15 +     $o.copyto join-path $o.directory.tostring() "all_v4_1.csv" +               ~~~~~~~~~ unexpected token 'join-path' in expression or statement.     + categoryinfo          : parsererror: (:) [], parentcontainserrorrecordexception     + fullyqualifiederrorid : unexpectedtoken 

what doing wrong here?


update, 20150604

as commented below manuel batsching, original version can fixed adding 2 layers of parentheses: 1 indicate function argument, , 1 force evaluation order:

$oldfiles = dir -recurse | ?{$_.name -eq "all_v4_0.csv"} foreach ($o in $oldfiles) {     $o.copyto((join-path $o.directory.tostring() "all_v4_1.csv")) } 

for problem @ hand, 1 of solutions .fullname.replace easier.

psh's parser not reading join-path , arguments expression evaluate , pass result outer expression. parentheses force evaluation order.

but additionally copyto .net member, rather psh cmdlet, needs have parentheses around argument (and no space).

thus:

$o.copyto((join-path $o.directory.tostring() "all_v4_1.csv")) 

(possibly using psh's copy-item cmdlet cleaner option.)


Comments