2017-08-06 52 views
0

當我使用shell命令行使用winzip時,在vba中使用命令行時,返回路徑和文件名的變量r和q不會識別爲字符串。還有什麼代碼,我需要跳過任何已經在目錄中的zip文件。使用WinZip在子文件夾中的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問:我返回S:\ upsdata \ eomonhands \雅培\ abbott.xlsx。我使用thata作爲調用winzip的命令行中的文件名,但它不會將其視爲字符串。如何將q作爲字符串返回。還有什麼代碼可以過濾掉該文件夾中已經有zip文件的其他文件。我不想壓縮這些。

+0

您肯定會在該命令中出現語法錯誤 - '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

+0

@ YowE3K好吧忘了winzip部分。當我到達objfile.Name它說對象需要。我重新編寫了代碼以包含子文件夾路徑。它不會顯示名稱。 – Atlas80808

+0

「它說對象需要當它擊中行ojbFile.Name」,真的嗎? '對於每個在colFiles objFile'應該保證它被填充! –

回答

0

要壓縮在被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),那麼在被調用時允許它是一個字符串。

相關問題