打开嵌套目录中的 zip 文件失败并出现 "unexpected token" 错误
Open zip files in nested directories fails with "unexpected token" error
我有一个脚本,我试图通过该脚本对目录进行排序并打开所有 zip 文件并将文本文件全部存储到一个目录中。这是代码:
#Script to open zip files in tree
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) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
}
我想我差不多搞定了,但不断收到此错误(任何关于管道的详细说明都会有所帮助):
Unexpected token 'in' in expression or statement.
At E:\Expand-ZIPFile.ps1:14 char:19
+ foreach($item in <<<< $_.items()){
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
编辑:
啊...谢谢你的区别。我进行了您的编辑,但在每次 "write-host" 检查文件名后,我都收到以下错误:
`您不能在空值表达式上调用方法。在 E:\Expand-ZIPFile.ps1:14 char:30 + foreach($item in $zip.items <<<< ()){ + CategoryInfo : InvalidOperation: (items:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
EDIT2:所以原始代码确实将文件复制到新目录,但也复制了整个 zip 文件。我试图添加一个 if 语句以仅复制 .txt 文件,但代码只是逐步遍历每个目录而不复制任何内容。如果您有任何想法,我将不胜感激,因为我已经用尽了所有想法。这是代码:
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.Extension -eq ".txt") {
$shell.Namespace($destination).copyhere($item)
}
}
}
你混淆了 foreach
with ForEach-Object
loops. The former neither read from nor do they write to pipelines. Also, $file
is a FileInfo
object, not a path. The NameSpace
方法需要路径字符串,所以你需要使用 $file.FullName
.
替换
$zip = $shell.NameSpace($file) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
和
$zip = $shell.NameSpace($file.FullName)
foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item)
}
我有一个脚本,我试图通过该脚本对目录进行排序并打开所有 zip 文件并将文本文件全部存储到一个目录中。这是代码:
#Script to open zip files in tree
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) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
}
我想我差不多搞定了,但不断收到此错误(任何关于管道的详细说明都会有所帮助):
Unexpected token 'in' in expression or statement.
At E:\Expand-ZIPFile.ps1:14 char:19
+ foreach($item in <<<< $_.items()){
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
编辑: 啊...谢谢你的区别。我进行了您的编辑,但在每次 "write-host" 检查文件名后,我都收到以下错误: `您不能在空值表达式上调用方法。在 E:\Expand-ZIPFile.ps1:14 char:30 + foreach($item in $zip.items <<<< ()){ + CategoryInfo : InvalidOperation: (items:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
EDIT2:所以原始代码确实将文件复制到新目录,但也复制了整个 zip 文件。我试图添加一个 if 语句以仅复制 .txt 文件,但代码只是逐步遍历每个目录而不复制任何内容。如果您有任何想法,我将不胜感激,因为我已经用尽了所有想法。这是代码:
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.Extension -eq ".txt") {
$shell.Namespace($destination).copyhere($item)
}
}
}
你混淆了 foreach
with ForEach-Object
loops. The former neither read from nor do they write to pipelines. Also, $file
is a FileInfo
object, not a path. The NameSpace
方法需要路径字符串,所以你需要使用 $file.FullName
.
替换
$zip = $shell.NameSpace($file) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
和
$zip = $shell.NameSpace($file.FullName)
foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item)
}