2017-03-16 111 views
1

我加载一个哈希表与此密钥:查找哈希表(的containsKey)不工作

$htA = dir | Where-Object {$_.Name -match "\.output\.[A-Z]-[0-9]\.csv"} | select name,length 

当我尝试搜索键列类似正是如此:

$htA.ContainsKey($h.name) 

我得到这个超级整型错误:

方法调用失败,因为[Selected.System.IO.FileInfo]不包含名为'ContainsKey'的方法。

At line:3 char:9 
IF ($htA.ContainsKey($h.name) -eq $false) { 

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : InvalidOperation: (ContainsKey:String) [], RuntimeException 
+ FullyQualifiedErrorId : MethodNotFound** 

那么这不是一个合法的哈希表?

回答

2

$ htA被创建为类型转换为它,在这种情况下System.IO.FileInfo。试试这个:

$htA = @{} # Initialises as an empty hash table 
foreach ($file in (dir | Where-Object {$_.Name -match "\.output\.[A-Z]-[0-9]\.csv"} | select name,length)) 
{ 
$htA.Add($file.name, $file.length) # Populate hash table 
}