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 - Process all the files

Reply
 
Old 10-21-2008   #1 (permalink)
PSApple


 
 

Process all the files

I have down the general processing of a single file and I can rewrite the
original file.

get-content test.data | foreach{ $_ -replace "cat","dog" } | set-content
test.data

Now I can't wrap my head around the next pipeline jump and do this for all
the files..

gci | % { gc $_ | % {$_ -replace "dog","cat" } | Set-Content $_ }

thats rewritng ontop of itsself!

Can you one-liner these things?



My System SpecsSystem Spec
Old 10-21-2008   #2 (permalink)
PaulChavez


 
 

RE: Process all the files

gci | % { $file = $_; gc $file | % {$_ -replace "dog","cat" } | Set-Content
$file }

"PSApple" wrote:
Quote:

> I have down the general processing of a single file and I can rewrite the
> original file.
>
> get-content test.data | foreach{ $_ -replace "cat","dog" } | set-content
> test.data
>
> Now I can't wrap my head around the next pipeline jump and do this for all
> the files..
>
> gci | % { gc $_ | % {$_ -replace "dog","cat" } | Set-Content $_ }
>
> thats rewritng ontop of itsself!
>
> Can you one-liner these things?
>
>
>
My System SpecsSystem Spec
Old 10-21-2008   #3 (permalink)
tojo2000


 
 

Re: Process all the files

On Oct 21, 1:35*pm, "PSApple" <mapple...@xxxxxx> wrote:
Quote:

> I have down the general processing of a single file and I can rewrite the
> original file.
>
> get-content test.data | foreach{ $_ -replace "cat","dog" } | set-content
> test.data
>
> Now I can't wrap my head around the next pipeline jump and do this for all
> the files..
>
> gci *| % { gc $_ | % {$_ -replace "dog","cat" } | Set-Content $_ *}
>
> thats rewritng ontop of itsself!
>
> Can you one-liner these things?
I think your problem is that you're assuming that $_ means the same
thing down the line. The first time you use $_ it has a file as its
value. After you use Get-Content, though, each subsequent $_ refers
to an individual line.

dir | %{$filename = $_.Name; gc $_ | %{$_ -replace 'dog', 'cat'} | Set-
Content $filename}

should work, but I haven't tested it. The important thing is to keep
in mind what the $_ means in each case.

In a case like this it might even be more readable just to give up on
the one-liner and do a

foreach ($file in dir) {
gc $file | %{$_ -replace 'dog', 'cat'} | Set-Content $_
}

Readability goes a long way, especially if you ever want to pass your
knowledge along.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
is there a way to process large files in powershell? PowerShell
Failover Guest Cluster -- 'The process cannot access the file becauseit is being used by another process.' Virtual Server
Copying files, then hang, and unable to abort and kill the process. Vista networking & sharing
conime process is locking files and directories Vista account administration
Bug? Shouldn't Stop-Process automatically match Id if object is a process? PowerShell


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