【问题标题】:Zip Files in Subfolders Using WinZip使用 WinZip 压缩子文件夹中的文件
【发布时间】:2017-08-06 21:24:23
【问题描述】:

当我使用 shell 命令行来使用 winzip 时,在 vba 中使用命令行时,返回路径和文件名的变量 r 和 q 无法识别为字符串。我还需要什么代码来跳过目录中已经存在的任何 zip 文件。

Option Explicit
Public Function ZipAll()
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.File
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "S:\UPSData\EOMOnHand\Abbott\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Function

Public Function ShowSubFolders(Folder)
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.File
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Dim r As String
Dim q As String
Dim NextRow As Long
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    NextRow = 1

    For Each Subfolder In Folder.Subfolders
    'MsgBox SubFolder.Path
    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
    r = Subfolder.Path & "\" & objFile.Name & ".zip"
    q = Subfolder.Path & "\" & objFile.Name
    MsgBox r
    MsgBox q
    Shell "C:\Program Files\WinZip\WinZip64.exe -min -a " & r & " " & q
    NextRow = NextRow + 1
    Next
Next
End Function

当我 msgbox q 我返回 s:\upsdata\eomonhands\abbott\abbott.xlsx。我在调用 winzip 的命令行中使用 thata 作为文件名,但它不会将其视为字符串。如何将 q 作为字符串返回。还有什么代码可以过滤掉该文件夹中已经是 zip 文件的任何其他文件。我不想压缩那些。

【问题讨论】:

  • 您肯定在该命令中得到了语法错误 - C:\Program Files\WinZip\WinZip64.exe -min -as:\UPSData\EOMOnHand\Abbott\xyz\abc.xlsxs:\UPSData\EOMOnHand\Abbott\xyz\*.xlsx 将无效,因为 -as:\UPSData\EOMOnHand\Abbott\xyz\abc.xlsxs:\UPSData\EOMOnHand\Abbott\xyz\*.xlsx 不是一个合理的参数(但我没有安装 WinZip 来检查正确的语法是)
  • @YowE3K 好的,忘记 winzip 部分。当我到达 objfile.Name 时,它​​说需要对象。我重新编写了代码以包含 subolders 路径。它不会显示名称。
  • “当它到达 ojbFile.Name 行时,它说 Object Require”,真的吗? For Each objFile in colFiles 应该保证它被填充了!
  • rq 定义在哪里?请在代码顶部添加Option Explicit 以强制声明变量吗?
  • 代码(WinZip 问题除外)对我有用。

标签: vba winzip


【解决方案1】:

要压缩 xlsx 子文件夹中的所有文件,您可以执行以下操作:

Public Function ZipAll()
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.Files
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "S:\UPSData\EOMOnHand\Abbott\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Function

Public Function ShowSubFolders(Folder)
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.Files
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Dim FileName As String
Dim r As String
Dim q As String
Dim NextRow As Long
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    NextRow = 1

    For Each Subfolder In Folder.Subfolders
    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
    FileName = Subfolder.Path & "\" & objFile.Name
    If Right(FileName, 5) = ".xlsx" Then
    GoTo Line1
    Else
    GoTo Line2
    End If

Line1:
    r = Chr(34) & Subfolder.Path & "\" & objFile.Name & ".zip" & Chr(34)
    q = Chr(34) & Subfolder.Path & "\" & "*.xlsx" & Chr(34)
    iRunTaskID = Shell("C:\Program Files\WinZip\WinZip64.exe -min -a " & r & " " & q)
    DoEvents
    hProc = OpenProcess(SYNCHRONIZE, 0, iRunTaskID)
    If hProc <> 0 Then
    WaitForSingleObject hProc, INFINITE
    CloseHandle hProc
End If
    r = Subfolder.Path & "\" & objFile.Name & ".zip"
    If Len(Dir$(r)) > 0 Then
    GoTo Line3
    Else
    GoTo Line2
    End If
Line3:
bfile = Subfolder.Path & "\" & objFile.Name
If Len(Dir$(bfile)) > 0 Then
 Kill bfile
End If
Line2:
    NextRow = NextRow + 1
    Next
Next

End Function

基本上,如果您将过滤器放在文件名上,它将跳转到压缩文件的行并压缩该文件,而不是跳过列出的其他文件。如果你在 winzp 命令行中使用 chr(34) 允许它在调用时是一个字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    相关资源
    最近更新 更多