powershell - How to replace multiple strings in a text file and generate the logfile simultaneously? -


there multiple text files in folder , there multiple affected ids in each text file. need replace them new ids. also, want generate text log file listing filename,oldid,newid . log file required crosschecking , validation. have csv file creating array idlist. have listed part of code replacing string below.

foreach ($f in gci -r -include "*.txt") { set-variable -name filename -value ($f.name)   for( $i=0; $i -le $elementcount; $i++)  { write-host $i $oldstring= $idlist[$i].old_id+','+$idlist[$i].old_id_type $newstring= $idlist[$i].new_id+','+$idlist[$i].new_id_type   gc $f.fullname|foreach-object {if($_.contains($oldstring)){ "$filename,$oldstring,$newstring" >> $logfile; $_ -replace $oldstring,$newstring}}| sc $f.fullname   }   } 

i getting error :

set-content : process cannot access file 'u:\testscript\rapg6785.txt' because being used process. @ line:22 char:152 +      gc $f.fullname|foreach-object {if($_.contains($oldstring)){ "$filename,$oldstring,$newstring" >> $logfile; $_ -replace $oldstring,$newstring}}| sc <<<<  $f.fullname + categoryinfo          : notspecified: (:) [set-content], ioexception + fullyqualifiederrorid : system.io.ioexception,microsoft.powershell.commands.setcontentcommand 

try putting gc $f.fullname in brackets: (gc $f.fullname).

in way pipeline starts when gc ends read file , free file handle used process.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -