【发布时间】:2015-07-01 22:13:17
【问题描述】:
我有以下代码来解压缩目录树中的许多文件夹:
new-Item E:\Files -type directory
Get-Childitem -path E:\SNL_Insurance\* -recurse -exclude "*.md5" |
Foreach-object {
$file = $_
write-host $file;
$destination = "E:\Files"
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file.Fullname)
foreach($item in $zip.items()){
if ($item.name -eq ".txt") {
$shell.Namespace($destination).copyhere($item)
}
}
}
如果没有 if 语句,脚本也会复制 zip 文件,而不仅仅是下面包含的文本文件。我想我可以检查以确保文件扩展名是 .txt (或 *.txt 我都试过了),但 $item.name 似乎不包含我认为的内容。如果您有任何想法和/或可以在此处解释 $shell 变量(特别是 $shell.Namespace 这将非常有帮助),请提前致谢。
编辑:感谢您的回复。在我看到这些答案之前,我实际上找到了另一种方法。如果有人感兴趣,这是我找到的解决方案:
new-Item E:\Files -type directory
Get-Childitem -path E:\SNL_Insurance\Insurance\* -recurse -exclude "*.md5"|
Foreach-object {
if ($_.extension -eq ".zip") {
Get-Childitem -path $_ -recurse|
Foreach-object {
$file = $_
write-host $file;
$destination = "E:\Files"
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file.Fullname)
foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item)
}
}
}
}
【问题讨论】:
-
您可能正在寻找
$item.extension -
@RyanBemrose
FolderItem对象没有这样的属性。
标签: powershell