【发布时间】:2014-04-25 12:55:27
【问题描述】:
在 Access VBA 中,我整理了一个过程来执行此操作:
- 允许用户选择 zip 文件
- 将 zip 文件中的所有文件解压缩到同一目录(在此 特定的用例实例,它总是从 Zip 文件,从不进行任何更改,并且始终使用相同的密码)
- 然后我希望代码在解压后删除 Zip 文件 .xls 文件。
除了删除文件部分外,一切都很好。问题是当我告诉它删除“FileName.Zip”时,它正在删除“FileName.Zip”和“FileName.xls”
有什么方法可以确保他的 kill 命令只删除我想要删除的内容?我以前在各种场合都用过,从来没有发生过这种情况。
这是我正在使用的代码:
Dim fd As FileDialog
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tblProjectPath")
Set fd = FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
fd.Title = "Select TOC Return File(s) to process"
fd.InitialFileName = rs.Fields("ProjectPath") & "\FilingReports\*.zip"
fd.Show
For Each i In fd.SelectedItems
'Debug.Print i
Debug.Print '------------------------'
Debug.Print i
Unzip (i) 'The bit calling the command line unzip utility to unzip the file - just telling it to extract all files to the current folder.
Debug.Print i
'Kill i
'had to take out the kill bit, b/c it was deleting both the .zip and .xls files which is not desired nor expected
If InStr(i, ".zip") Then
Kill i 'Tried to specify only .zip files even though think I shouldn't need to, but it's still deleting .xls files
End If
Next i
编辑:添加解压缩代码以发布: 解压码:
Sub Unzip(Path As String)
Dim strUnzip As String
Dim QU As String 'quotation mark
QU = Chr(34)
strUnzip = QU & "c:\program files (x86)\winzip\wzunzip" & QU & " -s" & _
"ZipPassword " & _
Path & " " '& _
Call Shell(strUnzip)
End Sub
【问题讨论】:
-
你的 'If InStr(i, ".zip") Then' 不正确...应该是 'If InStr(1, i, ".zip") Then' 你的代码总是真的。
-
其实代码是正确的。 1 是一个可选值,我不需要指定,根据内置帮助文件。但是谢谢你的尝试。可以肯定的是,我确实尝试了您的建议,但得到了相同的结果。我最终可能会放弃 kill 命令并改用 FileSystemObject。
-
您的“解压缩”函数中的代码是什么?我刚刚运行了你的代码,它只删除了一个文件(我手动解压缩了文件)。尝试以下操作:(a)在“解压缩”上放置一个断点; (b) 运行代码到断点,然后手动解压文件; (c) 跳过您的解压缩语句; (d) 通过“杀戮”; (e) 检查文件夹是否有误删除。
-
谢谢!是的,我会深入研究解压缩代码。它基本上只是运行对 wzunzip(WinZip 的命令行解压缩实用程序)的命令行调用。我稍后会在这里发布它..
-
在主帖中添加了解压缩码。 “Path”是文件路径,从“i”变体变量中提取,该变量从 FileDialog 中提取文件路径。
标签: vba file kill delete-file