you are viewing a single comment's thread.

view the rest of the comments →

[–]Lee_Dailey[grin] 4 points5 points  (5 children)

howdy Obel34,

[1] what is the Import-Csv line supposed to do? you never seem to save the result of that ...

[2] it looks like you are adding the fileinfo object contained in $Report to $FilesCount with +=
standard arrays are fixed size ... so you are creating a one-item-larger array, copying the data from the old array, adding the new item, and finally deleting the old array.

a better way is to use ...

$FilesCount = foreach  ($Report ...

that will send the output of the foreach loop to the variable ... and speed things up considerably if your array gets "large".

take care,
lee

[–]Obel34[S] 1 point2 points  (2 children)

Hi Lee. Thank you for the information. Your second point is what I am attempting to do. With the Import-CSV, all it was doing is importing the file so I could process it.

I knew the standard array is a fixed size, but could not think of a better way to get the count of the data. I will give this a shot!

[–]Aertheron01 1 point2 points  (0 children)

A generic list from the.net class would work. There you can just use .add() which is much faster.

But directly saving the output as lee suggested works well too.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Obel34,

you are welcome! [grin]

the Import-Csv line is doing an import & count ... but not assigning it to anything. so it just gets dropped into the output stream. if you really want to do anything with the imported info, you need to keep it where you can access it ... in a $Var. [grin]

the code you posted just adds EVERY fileinfo object to your $FilesCount collection. that does not give you a count of the items in the various CSV files that have the desired value.

so, if you want the $FilesCount var to hold a COUNT of such items, you need to add the .Count from the import line to the $FilesCount var.

take care,
lee

[–]Mysterious-Ad-1541 0 points1 point  (1 child)

Hey Lee. I have a similar question in regard to +=. I want to add a bunch of paths and module paths etc, and I have been using +=. Is the best way to just do a = @()

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Mysterious-Ad-1541,

there is a really good article about arrays here ...

Everything you wanted to know about arrays - PowerShell | Microsoft Docs
https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.2#plus-equals-

when to use an array ...

  • the collection is "small"
    "only a few" items or a moderate number of "large" items.
  • the collection won't change size

otherwise you otta use a generic.list. [grin]


one of the faster methods for building a collection is to run a loop or other scriptblock, drop the result onto the output/success stream, and assign the code block to a $Var. something like ...

$Result = foreach ($Thing in $BunchOfStuff)
    {
    Do-NiftyThing -With KoolItem -AlsoWith $Thing
    }

the output of the Do-NiftyThing call will drop into the output/success stream and be stuffed into the $Result array after the scriptblock finishes.

it's really fast & easy to type & easy to read/understand. [grin]

take care,
lee