我已经做了相当多的搜索,但似乎找不到这个问题的答案,但如果它已经被回答,我道歉,如果你可以的话就链接我。
我正在尝试做的是根据当前路径中包含的文件数量最少,将文件分布在6个不同的路径上。
我想要做的是将来自这些对象的响应($Queue1-6只是文件路径)添加到一个数组中,然后对它们进行排序,并从第一个对象获得路径。
$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问题是(据我所知)在将这些值添加到数组之后,对象丢失了,剩下的只有值,所以现在我不知道如何引用原始对象来获得路径信息。
任何关于如何做到这一点的想法都将不胜感激,如果有更简单的方法来比较这些文件计数值并获得最低值的路径信息,那么就不需要像这样使用数组。
此外,如果有多个路径的值最低,则返回哪个路径都无关紧要。
提前感谢您的帮助。
发布于 2014-08-22 03:14:41
请注意,我已经使用了一些我自己的文件夹来代替$queue。此外,根据这些文件夹的位置,您可能能够构建一个简单的for each循环,即:如果它们都是同一父文件夹的子文件夹。
$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解释最后一行
需要.GetEnumerator()才能对哈希表进行排序。Sort-Object在默认情况下是升序的,所以没有必要提及它。Select-Object -First 1如果你不关心你得到的是哪一个,只要它有最少的文件就行了。-ExpandProperty Name,因为您实际上只需要路径,而不是哈希表条目本身。
https://stackoverflow.com/questions/25433330
复制相似问题