![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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? 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 Specs![]() |
![]() |
| 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 | |||