gpt4 book ai didi

vbscript - 使用 VBScript 从 ZIP 文件中提取文件

转载 作者:行者123 更新时间:2023-12-02 06:28:44 25 4
gpt4 key购买 nike

从 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

这是有效的,但现在我收到“文件存在”错误。

这是什么原因呢?还有其他选择吗?

最佳答案

以上所有解决方案都是准确的,但不是确定的。

如果您尝试将压缩文件提取到临时文件夹中,将立即创建一个显示“Temporary Folder For YOURFILE.zip”的文件夹(位于C:\DocumentsSettings\USERNAME\Local Settings\Temp),用于您尝试提取的 ZIP 文件中包含的每个文件

没错,如果您有 50 个文件,它将在您的临时目录中创建 50 个文件夹。

但是如果您有 200 个文件,它将停止在 99 个文件并崩溃并提示 - 文件存在

..

显然,根据我在上面查看的贡献,这种情况不会发生在 Windows 7 上。但无论如何,我们仍然可以进行检查。好的,这就是修复它的方法:

    '========================
'Sub: UnzipFiles
'Language: vbscript
'Usage: UnzipFiles("C:\dir", "extract.zip")
'Definition: UnzipFiles([Directory where zip is located & where files will be extracted], [zip file name])
'========================
Sub UnzipFiles(folder, file)
Dim sa, filesInzip, zfile, fso, i : i = 1
Set sa = CreateObject("Shell.Application")
Set filesInzip=sa.NameSpace(folder&file).items
For Each zfile In filesInzip
If Not fso.FileExists(folder & zfile) Then
sa.NameSpace(folder).CopyHere(zfile), &H100
i = i + 1
End If
If i = 99 Then
zCleanup(file, i)
i = 1
End If
Next
If i > 1 Then
zCleanup(file, i)
End If
fso.DeleteFile(folder&file)
End Sub

'========================
'Sub: zCleanup
'Language: vbscript
'Usage: zCleanup("filename.zip", 4)
'Definition: zCleanup([Filename of Zip previously extracted], [Number of files within zip container])
'========================
Sub zCleanUp(file, count)
'Clean up
Dim i, fso
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 1 To count
If fso.FolderExists(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file) = True Then
text = fso.DeleteFolder(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file, True)
Else
Exit For
End If
Next
End Sub

就是这样,将这两个函数复制并粘贴到您的 VBScript 托管程序中,您就可以在 Windows XP 和 Windows 7 上开始使用了。

谢谢!

关于vbscript - 使用 VBScript 从 ZIP 文件中提取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/291406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com