2017-08-06 105 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),那么在被调用时允许它是一个字符串。

相关问题