0

I've done quite a bit of searching but can't seem to find an answer to this, but if it has been answered, I apologize, and just link me to it if you can.

What I'm trying to do is distribute files across 6 different paths based on which path currently has the least number of files in it.

What I thought to do is to add the responses from these ($Queue1-6 are just file paths) to an array and then sort them and get the path from the first object.

$QueueFiles1 = ( Get-ChildItem $Queue1 | Measure-Object ).Count

$QueueFiles2 = ( Get-ChildItem $Queue2 | Measure-Object ).Count

$QueueFiles3 = ( Get-ChildItem $Queue3 | Measure-Object ).Count

$QueueFiles4 = ( Get-ChildItem $Queue4 | Measure-Object ).Count

$QueueFiles5 = ( Get-ChildItem $Queue5 | Measure-Object ).Count

$QueueFiles6 = ( Get-ChildItem $Queue6 | Measure-Object ).Count

$FileNumArray = @($QueueFiles1, $QueueFiles2, $QueueFiles3, $QueueFiles4, $QueueFiles5, $QueueFiles6)

$FileNumArray = $FileNumArray | Sort-Object

The problem is (as far as I can tell) that after adding these values to the array, the object is lost and all that is left is the value, so now I don't know how to reference back to the original object to obtain the path information.

Any thoughts on how to do this would be appreciated and it doesn't need to be done with an array, like this, if there's an easier way to compare those file count values and obtain the path information of the lowest value.

Also, if there is more than 1 path with the lowest value, it doesn't matter which is returned.

Thanks in advance for any assistance.

1
  • $QueueFiles1 = ( Get-ChildItem $Queue1 | Measure-Object ).Count all you are capturing is the count. The is no object assigned here. I am building an answer using hash tables. Commented Aug 21, 2014 at 19:03

1 Answer 1

1

Note that i have used some of my own folder to take place of the $queue(s). Also depending on the location of these your might be able to build a simple for each loop ie: if they were all subfolders of the same parent.

$Queue1 = "C:\Temp\NewName"
$Queue2 = "C:\temp\TaskManagement"
$Queue3 = "C:\temp\message_log.csv"

# Build a hashtable. Add each queue to the hash table. 
$fileCount = @{}
# Set the queue as the name and the count as the value
$fileCount.Add("$Queue1", (Get-ChildItem $Queue1 | Measure-Object ).Count)
$fileCount.Add("$Queue2", (Get-ChildItem $Queue2 | Measure-Object ).Count)
$fileCount.Add("$Queue3", (Get-ChildItem $Queue4 | Measure-Object ).Count)

# Sort the results by the value of the hashtable (Counts from earlier) and select only the one. 
$fileCount.GetEnumerator() | Sort-Object value | Select-Object -First 1 -ExpandProperty Name

Explaining the last line

.GetEnumerator() is required in order to sort the hashtable. Sort-Object is Ascending by default so there is no need to mention it. Select-Object -First 1 if you dont care which one you get as long as it has the smallest amount of files. -ExpandProperty Name since you only really need the path and not the hashtable entry itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.