首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从数组中的值引用原始对象?

如何从数组中的值引用原始对象?
EN

Stack Overflow用户
提问于 2014-08-22 02:19:17
回答 1查看 52关注 0票数 0

我已经做了相当多的搜索,但似乎找不到这个问题的答案,但如果它已经被回答,我道歉,如果你可以的话就链接我。

我正在尝试做的是根据当前路径中包含的文件数量最少,将文件分布在6个不同的路径上。

我想要做的是将来自这些对象的响应($Queue1-6只是文件路径)添加到一个数组中,然后对它们进行排序,并从第一个对象获得路径。

代码语言:javascript
复制
$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

问题是(据我所知)在将这些值添加到数组之后,对象丢失了,剩下的只有值,所以现在我不知道如何引用原始对象来获得路径信息。

任何关于如何做到这一点的想法都将不胜感激,如果有更简单的方法来比较这些文件计数值并获得最低值的路径信息,那么就不需要像这样使用数组。

此外,如果有多个路径的值最低,则返回哪个路径都无关紧要。

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-08-22 03:14:41

请注意,我已经使用了一些我自己的文件夹来代替$queue。此外,根据这些文件夹的位置,您可能能够构建一个简单的for each循环,即:如果它们都是同一父文件夹的子文件夹。

代码语言:javascript
复制
$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,因为您实际上只需要路径,而不是哈希表条目本身。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25433330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档