我正在尝试遍历文件夹中的zip文件并解压缩它们。我收到了zip.items()
的空错误。这个值怎么会是空的呢?
当I Write-Host $zip
时,发布的值为System.__ComObject
。
$dira = "D:\User1\Desktop\ZipTest\IN"
$dirb = "D:\User1\Desktop\ZipTest\DONE\"
$list = Get-childitem -recurse $dira -include *.zip
$shell = new-object -com shell.application
foreach($file in $list)
{
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
$shell.Namespace($dirb).copyhere($file)
}
Remove-Item $file
}
我收到的错误消息是:
You cannot call a method on a null-valued expression.
At D:\Users\lr24\Desktop\powershellunziptest2.ps1:12 char:29
+ foreach($item in $zip.items <<<< ())
+ CategoryInfo : InvalidOperation: (items:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
3条答案
按热度按时间dz6r00yl1#
$file
是FileInfo
对象,但NameSpace()
方法需要具有完整路径的字符串或数字常量。此外,您需要复制$item
,而不是$file
。更改此内容:
变成这样:
kq4fsx7k2#
如果$env:path中有7-zip
你可以在这里得到7-zip:
说明:
后面跟大括号的percent称为foreach运算符:%{}此运算符表示管道中的"Foreach"对象,调用大括号中的代码时将对象放在"$_"变量中。
n3h0vuf23#
你错过了shell初始化。
在命名空间之前使用它。