使用 VBScript 从 ZIP 文件中提取文件
从 ZIP 文件中提取文件时,我使用了以下命令。
Sub Unzip(strFile)
' This routine unzips a file. NOTE: The files are extracted to a folder '
' in the same location using the name of the file minus the extension. '
' EX. C:\Test.zip will be extracted to C:\Test '
'strFile (String) = Full path and filename of the file to be unzipped. '
Dim arrFile
arrFile = Split(strFile, ".")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(arrFile(0) & "\ ")
pathToZipFile= arrFile(0) & ".zip"
extractTo= arrFile(0) & "\ "
set objShell = CreateObject("Shell.Application")
set filesInzip=objShell.NameSpace(pathToZipFile).items
objShell.NameSpace(extractTo).CopyHere(filesInzip)
fso.DeleteFile pathToZipFile, True
Set fso = Nothing
Set objShell = Nothing
End Sub 'Unzip
这是有效的,但现在我收到“文件存在”错误。
这是什么原因呢? 还有其他选择吗?
When extracting files from a ZIP file I was using the following.
Sub Unzip(strFile)
' This routine unzips a file. NOTE: The files are extracted to a folder '
' in the same location using the name of the file minus the extension. '
' EX. C:\Test.zip will be extracted to C:\Test '
'strFile (String) = Full path and filename of the file to be unzipped. '
Dim arrFile
arrFile = Split(strFile, ".")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(arrFile(0) & "\ ")
pathToZipFile= arrFile(0) & ".zip"
extractTo= arrFile(0) & "\ "
set objShell = CreateObject("Shell.Application")
set filesInzip=objShell.NameSpace(pathToZipFile).items
objShell.NameSpace(extractTo).CopyHere(filesInzip)
fso.DeleteFile pathToZipFile, True
Set fso = Nothing
Set objShell = Nothing
End Sub 'Unzip
This was working, but now I get a "The File Exists" Error.
What is the reason for this? Are there any alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上述所有解决方案都是准确的,但它们不是确定的。
如果您尝试将压缩文件解压到临时文件夹中,将立即创建一个显示“Temporary Folder For YOURFILE.zip”的文件夹(位于
C:\Documents
和Settings\USERNAME\Local Settings\Temp
),用于您尝试提取的 ZIP 文件中包含的每个文件。没错,如果您有 50 个文件,它将在您的临时目录中创建 50 个文件夹。
但是如果您有 200 个文件,它将停止在 99 个文件并崩溃并提示 - 文件存在。
..
显然,根据我在上面查看的贡献,这种情况不会发生在 Windows 7 上。 但无论如何,我们仍然可以进行检查。 好吧,这就是修复它的方法:
就是这样,将这两个函数复制并粘贴到您的 VBScript 托管程序中,您应该可以在 Windows XP 和 Windows 上开始使用了。 Windows 7。
谢谢!
All above solutions are accurate, but they are not definitive.
If you are trying to extract a zipped file into a temporary folder, a folder that displays "Temporary Folder For YOURFILE.zip" will immediately be created (in
C:\Documents
andSettings\USERNAME\Local Settings\Temp
) for EACH FILE contained within your ZIP file, which you are trying to extract.That's right, if you have 50 files, it will create 50 folders within your temp directory.
But if you have 200 files, it will stop at 99 and crash stating - The File Exists.
..
Apparently, this does not occur on Windows 7 with the contributions I view above. But regardless, we can still have checks. Alright, so this is how you fix it:
And that's it, copy and paste those two functions into your VBScript hosted program and you should be good to go, on Windows XP & Windows 7.
Thanks!
您可以从 VBScript 使用 DotNetZip。
要解压现有的 zip 文件,覆盖可能存在的任何文件:
要创建新的 zip 文件:
You can use DotNetZip from VBScript.
To unpack an existing zipfile, overwriting any files that may exist:
To create a new zipfile:
上面的答案是完全正确的,但我想我应该将所有内容都包含到我正在使用的完整解决方案中:
There's answers above which are perfectly correct, but I thought I'd wrap everything up into a full solution that I'm using:
http://www.experts-exchange.com/Programming/ Languages/Visual_Basic/VB_Script/Q_23022290.html
检查您的临时目录。 如果您有 99 个与此解压缩过程关联的文件夹,请尝试删除它们。
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23022290.html
Check your temp directory. If you have 99 folders associated with this unzipping process, try deleting them.
我将以下代码添加到解压缩过程的开头,以在解压缩之前删除这些目录:
I added the following code to the beginning of my unzip procedure to delete these directories before I unzip: