![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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. > 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 > Mmm perhaps Log Parser data parsing and charting! Quote: > (and yes the example is pointless) > 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 Specs![]() |
| | #5 (permalink) |
| | 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 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||