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?
powershell
add a comment |
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?
powershell
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
add a comment |
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?
powershell
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
powershell
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 9 at 22:15
Lee_Dailey
98266
98266
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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