woensdag 28 januari 2015

VBA: Zip Copier

Ran into a problem of moving files contained in several zip files to different folders.

This is part of the code I used. It moves the files out of a zip file called test.zip (any name + .zip would do) in folder d:\data\start to a destination folder called d:\data\end. It would also work if d:\data\start contained more zip files.

Sub zipcopier()
    Dim fs As Object, f As Object
    Dim FileInFolder As Object, sh As Object, ZipFile As Object, fileInZip As Object
    Dim FolderTo As Variant, FolderFrom As Variant

    FolderTo = "d:\data\end"
    FolderFrom = "d:\data\start"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(FolderFrom)
    'procedure for zip-files
    For Each FileInFolder In f.Files
        If Right(FileInFolder.Name, 4) = ".zip" Then
            Set sh = CreateObject("Shell.Application")
            Set ZipFile = sh.Namespace(f & "\" & FileInFolder.Name)
            For Each fileInZip In ZipFile.items
                'Create folder if not there already
                If Not fs.FolderExists(FolderTo) Then
                    fs.CreateFolder (FolderTo)
                End If

                'copies files from zip to destination one by one
                If Not fs.fileexists(FolderTo & "\" & Mid(fileInZip.Path, InStrRev(fileInZip.Path, "\") + 1)) Then
                   sh.Namespace(FolderTo).moveHere (fileInZip.Path)
                End If
        End If
End Sub
Een reactie posten