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 - Powershell - outer variable

Reply
 
Old 01-12-2009   #1 (permalink)
Matt Freeman


 
 

Powershell - outer variable

get-childitem -recurse
| where { $_.Extension -eq ".csproj" }
| foreach { Get-Content $_.FullName
| foreach { $_.Length } }

This prints the line size of every line in a csproj (pretty pointless true).
How can I also output a outer variable (so to speak) when I've dived further.
So for example let's say for pointeless reasons I wanted to have it print the
filename too so I would get:

Dog.csproj: 10 Dog.csproj: 50 Dog.csproj: 4 Cat.csproj: 100 Cat.csproj: 440

I figure I want to do something like this but this does not work obviously,
(and yes the example is pointless)

PS C:\Projects>
get-childitem -recurse
| STORE THIS IN $filename | where { $_.Extension -eq ".csproj" }
| foreach { Get-Content $_.FullName
| foreach { $filename ":" $_.Length } }

I played with tee-object and outputvariable but I'm a bit lost. If a
powershell guru could answer it would help, also if you could recommend a
book or resource that explains the language syntax fundamentals rather than
API monkey stuff of COM/WMI/VB etc.. (that seems most of what I came across)
it would be most appreciated. Thanks

My System SpecsSystem Spec
Old 01-12-2009   #2 (permalink)
Matthias Tacke


 
 

Re: Powershell - outer variable

Matt Freeman wrote:
Quote:

> Marco,
>
> Thank but I'm actually diving further in if you see my length represents the
> length of a line (i.e. we moved further on from the file). Essentially I
> want to assigned a variable during the sequence flow so I can reference the
> outer variable.
>
I don't know if it's possible to gain direct access to elements of an outer
foreach, but with an intermediate var this should do:

gci -rec -inc *.csproj|foreach{$a=$_.name;gc $_.fullname|foreach{
$a+":"+$_.length}}

HTH
Matthias
My System SpecsSystem Spec
Old 01-12-2009   #3 (permalink)
Shay Levy [MVP]


 
 

Re: Powershell - outer variable

Hello Matt,

Ths will output the file name with a collection of line lengths in the Lines
column:

get-childitem -recurse -filter *.csproj | select @{n="FileName";e={$.FullName}},@{n="Lines";e={
$(cat $_.FullName).count}}


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


MM> get-childitem -recurse
MM> | where { $_.Extension -eq ".csproj" }
MM> | foreach { Get-Content $_.FullName
MM> | foreach { $_.Length } }
MM> This prints the line size of every line in a csproj (pretty
MM> pointless true). How can I also output a outer variable (so to
MM> speak) when I've dived further. So for example let's say for
MM> pointeless reasons I wanted to have it print the filename too so I
MM> would get:
MM>
MM> Dog.csproj: 10 Dog.csproj: 50 Dog.csproj: 4 Cat.csproj: 100
MM> Cat.csproj: 440
MM>
MM> I figure I want to do something like this but this does not work
MM> obviously, (and yes the example is pointless)
MM>
MM> PS C:\Projects>
MM> get-childitem -recurse
MM> | STORE THIS IN $filename | where { $_.Extension -eq ".csproj"
MM> }
MM> | foreach { Get-Content $_.FullName
MM> | foreach { $filename ":" $_.Length } }
MM> I played with tee-object and outputvariable but I'm a bit lost. If a
MM> powershell guru could answer it would help, also if you could
MM> recommend a book or resource that explains the language syntax
MM> fundamentals rather than API monkey stuff of COM/WMI/VB etc.. (that
MM> seems most of what I came across) it would be most appreciated.
MM> Thanks
MM>


My System SpecsSystem Spec
Old 01-12-2009   #4 (permalink)
Flowering Weeds


 
 

Re: Powershell - outer variable

Quote:

>
> This prints the line size of every line
> in a csproj (pretty pointless true).
Quote:

>
> Dog.csproj: 10 Dog.csproj: 50 Dog.csproj: 4
> Cat.csproj: 100 Cat.csproj: 440
>
FYI

Mmm perhaps Log Parser
data parsing and charting!
Quote:

> (and yes the example is pointless)
>
Pointless -perhaps not at all,
for after doing little pointless
Log Parser data parsing
one might be ready for much larger
Log Parser data parsing!

$filename = "about_globbing.help.txt"
$title = "Char counts information for`n$filename"

LogParser.exe "SELECT
REPLACE_IF_NULL(STRLEN(Text), 0) AS CharsPerLine,
COUNT(*) AS LineCountsWithEqualCharCounts
INTO testChart.gif
FROM '$pshome\$filename'
GROUP BY CharsPerLine
ORDER BY LineCountsWithEqualCharCounts DESC " `
-i textline -stats off -o chart `
-values on -chartType Area -chartTitle $title

invoke-item testChart.gif

Exit

For more help, ask any PowerShell user or MVP or
any IIS user or MVP (where Log Parser comes from,
notice that there is no need to have IIS installed in
order to use Log Parser within the Windows admin's
automation tool, powershell.exe) or just perhaps:

LogParser.exe -h

Have fun data parsing
and perhaps charting
within powershell.exe!


My System SpecsSystem Spec
Old 01-12-2009   #5 (permalink)
BJ


 
 

Re: Powershell - outer variable

On 12 Jan., 13:09, Matt Freeman <Matt
Free...@xxxxxx> wrote:
Quote:

> * *get-childitem *-recurse
> *| *where { $_.Extension -eq ".csproj" }
> *| foreach { Get-Content $_.FullName
> * * * * * | foreach { $_.Length } }
>
> This prints the line size of every line in a csproj (pretty pointless true).
> How can I also output a outer variable (so to speak) when I've dived further.
> So for example let's say for pointeless reasons I wanted to have it printthe
> filename too so I would get:
>
> Dog.csproj: 10 Dog.csproj: 50 Dog.csproj: 4 Cat.csproj: 100 Cat.csproj: 440
>
> I figure I want to do something like this but this does not work obviously,
> (and yes the example is pointless)
>
> * PS C:\Projects>
> * * * * get-childitem *-recurse
> * * *| *STORE THIS IN $filename | where { $_.Extension -eq ".csproj" }
> * * *| foreach { Get-Content $_.FullName
> * * * * * * * | foreach { $filename ":" *$_.Length } }
>
> I played with tee-object and outputvariable but I'm a bit lost. If a
> powershell guru could answer it would help, also if you could recommend a
> book or resource that explains the language syntax fundamentals rather than
> API monkey stuff of COM/WMI/VB etc.. (that seems most of what I came across)
> it would be most appreciated. Thanks
Is this what you are looking for?
Not sure if i see it right.

$files = get-childitem -recurse -filter *.csproj
foreach ($file in $files)
{
""
"Filename: {0}" -f $($file.Fullname)
"=" * ($($File.FullName.Length) + 10)
$content = gc $file.FullName
$i = 1
foreach ($line in $content)
{
" {2} Line {0,4}: {1,5} Chars" -f $i, $Line.Length, $($file.Name)
$i++
}
}

My System SpecsSystem Spec
Old 01-14-2009   #6 (permalink)
Kiron


 
 

Re: Powershell - outer variable

You'd need an temp variable for that -as Matthias and BJ already pointed out- but you can also get that data from the Microsoft.PowerShell.Commands.MatchInfo object Select-String returns:

Select-String '^' *.csproj |
% {"$($_.filename) [$($_.lineNumber)] : $($_.line.length)"}

I'd also recommend Bruce Payette's book.

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to perform variable substitution in powershell? PowerShell
Changing the PATH variable in the registry with Powershell PowerShell
Copying a VBS variable in powershell? PowerShell
Re: Copying a VBS variable in powershell? PowerShell
How can I ensure that a variable is a built-in powershell variable? 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