Read a directory listing into Powershell, then display every other filename to the screen









up vote
0
down vote

favorite












Here is the Powershell script that I have created so far:



dir C:WindowsSystem32 | Sort-Object -Descending 


My end goal is to list all the files (files only - no sub directories) in the following directory, sorted by filename in descending order. Next I need to read this directory listing created into Powershell, then display every other filename to the screen.



I was trying to find a way to do this by piping the sorted results into the Foreach-Object cmdlet, and then reading "Object" from the resulting list, using the Get-Content cmdlet, and the FullName proprety of each object - like: Get-Content $_.FullName



dir C:WindowsSystem32 | Sort-Object -Descending | ForEach-Object 


Any ideas how to make this happen with those cmdlets?










share|improve this question

















  • 1




    ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
    – LotPings
    Nov 9 at 23:08










  • Exactly what I was looking for. Thank you.
    – bear443
    Nov 11 at 23:57










  • You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
    – LotPings
    Nov 12 at 9:42














up vote
0
down vote

favorite












Here is the Powershell script that I have created so far:



dir C:WindowsSystem32 | Sort-Object -Descending 


My end goal is to list all the files (files only - no sub directories) in the following directory, sorted by filename in descending order. Next I need to read this directory listing created into Powershell, then display every other filename to the screen.



I was trying to find a way to do this by piping the sorted results into the Foreach-Object cmdlet, and then reading "Object" from the resulting list, using the Get-Content cmdlet, and the FullName proprety of each object - like: Get-Content $_.FullName



dir C:WindowsSystem32 | Sort-Object -Descending | ForEach-Object 


Any ideas how to make this happen with those cmdlets?










share|improve this question

















  • 1




    ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
    – LotPings
    Nov 9 at 23:08










  • Exactly what I was looking for. Thank you.
    – bear443
    Nov 11 at 23:57










  • You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
    – LotPings
    Nov 12 at 9:42












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Here is the Powershell script that I have created so far:



dir C:WindowsSystem32 | Sort-Object -Descending 


My end goal is to list all the files (files only - no sub directories) in the following directory, sorted by filename in descending order. Next I need to read this directory listing created into Powershell, then display every other filename to the screen.



I was trying to find a way to do this by piping the sorted results into the Foreach-Object cmdlet, and then reading "Object" from the resulting list, using the Get-Content cmdlet, and the FullName proprety of each object - like: Get-Content $_.FullName



dir C:WindowsSystem32 | Sort-Object -Descending | ForEach-Object 


Any ideas how to make this happen with those cmdlets?










share|improve this question













Here is the Powershell script that I have created so far:



dir C:WindowsSystem32 | Sort-Object -Descending 


My end goal is to list all the files (files only - no sub directories) in the following directory, sorted by filename in descending order. Next I need to read this directory listing created into Powershell, then display every other filename to the screen.



I was trying to find a way to do this by piping the sorted results into the Foreach-Object cmdlet, and then reading "Object" from the resulting list, using the Get-Content cmdlet, and the FullName proprety of each object - like: Get-Content $_.FullName



dir C:WindowsSystem32 | Sort-Object -Descending | ForEach-Object 


Any ideas how to make this happen with those cmdlets?







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:39









bear443

83




83







  • 1




    ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
    – LotPings
    Nov 9 at 23:08










  • Exactly what I was looking for. Thank you.
    – bear443
    Nov 11 at 23:57










  • You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
    – LotPings
    Nov 12 at 9:42












  • 1




    ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
    – LotPings
    Nov 9 at 23:08










  • Exactly what I was looking for. Thank you.
    – bear443
    Nov 11 at 23:57










  • You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
    – LotPings
    Nov 12 at 9:42







1




1




ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
– LotPings
Nov 9 at 23:08




ForEach-Object has an optional begin scriptblock you can use to initialize a counter, so append |ForEach-Object $i=0$i++;if($i%2)$_ to output all even entries.
– LotPings
Nov 9 at 23:08












Exactly what I was looking for. Thank you.
– bear443
Nov 11 at 23:57




Exactly what I was looking for. Thank you.
– bear443
Nov 11 at 23:57












You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
– LotPings
Nov 12 at 9:42




You don't even need a separatee increment command, the position of the ++ dertermines if it is pre or post the modulus. |ForEach-Object $i=0if(++$i%2)$_ or |ForEach-Object $i=0if($i++%2)$_
– LotPings
Nov 12 at 9:42












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










you can get the index number of an item in an array with .IndexOf(). you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...



$TargetDir = "$env:windirsystem32"

$FileList = Get-ChildItem -LiteralPath $TargetDir -File |
Sort-Object -Property Name

foreach ($FL_Item in $FileList)

# if "number mod 2 = zero", then it's an even number
if ($FileList.IndexOf($FL_Item) % 2 -eq 0)

$FL_Item.Name







share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233582%2fread-a-directory-listing-into-powershell-then-display-every-other-filename-to-t%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    you can get the index number of an item in an array with .IndexOf(). you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...



    $TargetDir = "$env:windirsystem32"

    $FileList = Get-ChildItem -LiteralPath $TargetDir -File |
    Sort-Object -Property Name

    foreach ($FL_Item in $FileList)

    # if "number mod 2 = zero", then it's an even number
    if ($FileList.IndexOf($FL_Item) % 2 -eq 0)

    $FL_Item.Name







    share|improve this answer
























      up vote
      0
      down vote



      accepted










      you can get the index number of an item in an array with .IndexOf(). you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...



      $TargetDir = "$env:windirsystem32"

      $FileList = Get-ChildItem -LiteralPath $TargetDir -File |
      Sort-Object -Property Name

      foreach ($FL_Item in $FileList)

      # if "number mod 2 = zero", then it's an even number
      if ($FileList.IndexOf($FL_Item) % 2 -eq 0)

      $FL_Item.Name







      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        you can get the index number of an item in an array with .IndexOf(). you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...



        $TargetDir = "$env:windirsystem32"

        $FileList = Get-ChildItem -LiteralPath $TargetDir -File |
        Sort-Object -Property Name

        foreach ($FL_Item in $FileList)

        # if "number mod 2 = zero", then it's an even number
        if ($FileList.IndexOf($FL_Item) % 2 -eq 0)

        $FL_Item.Name







        share|improve this answer












        you can get the index number of an item in an array with .IndexOf(). you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...



        $TargetDir = "$env:windirsystem32"

        $FileList = Get-ChildItem -LiteralPath $TargetDir -File |
        Sort-Object -Property Name

        foreach ($FL_Item in $FileList)

        # if "number mod 2 = zero", then it's an even number
        if ($FileList.IndexOf($FL_Item) % 2 -eq 0)

        $FL_Item.Name








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 22:15









        Lee_Dailey

        98266




        98266



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233582%2fread-a-directory-listing-into-powershell-then-display-every-other-filename-to-t%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20