none
Powershell: use of ++ and variables in a statement RRS feed

  • Question

  • Dear Experts:

    I know ++ adds 1 to the count so for e.g. if $ file = 10 then

         $file++ changes the value of the variable to 11.

    But I can't understand its application in this statement below because my (gci).count is 27, BUT  $filecount is giving me 54. Also the statement use $filecount instead of $_.

                            gci | foreach-object { $filecount++ $totsize+= $_.Length}

    Also i thought we have separate each item with commas or semicolon but if this statement assuming it is correctly written is not using commas after $filecount++ like in $filecount++, $totsize

    Can someone explain me what this command above is doing piece wise....

    Regards

    SQL75


    • Edited by SQL75 Wednesday, February 11, 2015 8:04 PM
    Wednesday, February 11, 2015 8:00 PM

Answers

  • Hi,

    Why do you keep putting everything on a single line?

    Add in some trace statements so you can see what is happening:

    $fileCount = 0
    $totalSize = 0
    
    Get-ChildItem | ForEach {
    
        Write-Host "Top of loop, fileCount is $fileCount"
        Write-Host "Top of loop, totalSize is $totalSize"
    
        $fileCount++
        $totalSize += $_.Length
    
        Write-Host "Bottom of loop, fileCount is $fileCount"
        Write-Host "Bottom of loop, totalSize is $totalSize"
    
    }

    I highly suggest that you click here and start with the very basics first:

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Edited by Mike Laughlin Wednesday, February 11, 2015 8:11 PM
    • Marked as answer by SQL75 Wednesday, February 11, 2015 8:17 PM
    Wednesday, February 11, 2015 8:09 PM
  • The most common use of the ++ operator is this:

    for($i=0;$i -lt 10; $i++){
        ....
    )

    "$i++" is shorthand for "$i+=1"  and "$i=$i+1"

    These are all standaaard programming construct brout into PowerShell from "C" and similar languages.

    I recommend getting a book on the basics of programming and starting there.  I will help you avoid months of going around in circles trying to guess at how this works.


    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Thursday, February 12, 2015 6:07 AM
    Thursday, February 12, 2015 1:02 AM

All replies

  • Hi,

    Why do you keep putting everything on a single line?

    Add in some trace statements so you can see what is happening:

    $fileCount = 0
    $totalSize = 0
    
    Get-ChildItem | ForEach {
    
        Write-Host "Top of loop, fileCount is $fileCount"
        Write-Host "Top of loop, totalSize is $totalSize"
    
        $fileCount++
        $totalSize += $_.Length
    
        Write-Host "Bottom of loop, fileCount is $fileCount"
        Write-Host "Bottom of loop, totalSize is $totalSize"
    
    }

    I highly suggest that you click here and start with the very basics first:

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Edited by Mike Laughlin Wednesday, February 11, 2015 8:11 PM
    • Marked as answer by SQL75 Wednesday, February 11, 2015 8:17 PM
    Wednesday, February 11, 2015 8:09 PM
  • Thanks Mike I will go through the video series you mentioned.

    Regards

    SQL 75

    Wednesday, February 11, 2015 8:38 PM
  • Wednesday, February 11, 2015 8:48 PM
  • The most common use of the ++ operator is this:

    for($i=0;$i -lt 10; $i++){
        ....
    )

    "$i++" is shorthand for "$i+=1"  and "$i=$i+1"

    These are all standaaard programming construct brout into PowerShell from "C" and similar languages.

    I recommend getting a book on the basics of programming and starting there.  I will help you avoid months of going around in circles trying to guess at how this works.


    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Thursday, February 12, 2015 6:07 AM
    Thursday, February 12, 2015 1:02 AM
  • Thanks JRV, your advise is much appreciated. My depth of programming knwoledge is limited at the moment to SQL and HTML/CSS. If you found some C book or programming book  to be useful for beginners by some specific author please let me know. I can add it to my to do list.

    Thank-you

    SQL 75

    Thursday, February 12, 2015 6:10 AM
  • I always recommend the classic.  Everyone who write any kind of code should read it.

    "The C Programming Language" by Kernaghan and Ritchie.

    Here is a copy of the second edition: http://books.cat-v.org/computer-science/c-programming-language/The.C.Programming.Language.2nd.Edition.pdf

    And "The Elements of Style" http://books.cat-v.org/computer-science/elements-of-programming-style/elements_of_programming_style.pdf

    These fundamentals are good programming background for any language.  These are two of the main books that every programmer should have on his shelf.

    For PowerShell there are many good books.  For beginners I prefer "PowerShell TFM 2nd Edition". you can get it used for a buck.  It is a good set of easy walk-throughs and is more comprehensive than "A Month of Lunches".

    There are many, many more but these are good as starters.

    You can do any of them in a long weekend.


    ¯\_(ツ)_/¯

    Thursday, February 12, 2015 6:48 AM
  • Thank-you  JRV for being a great resource!!

    Best Wishes,

    SQL75

    Saturday, February 14, 2015 4:10 AM
  • Thank-you  JRV for being a great resource!!

    Best Wishes,

    SQL75


    You are very welcome. I am glad it was useful.

    ¯\_(ツ)_/¯

    Saturday, February 14, 2015 4:30 AM