Solved

1) How can I use VBA to unpack a file from a zipfile that gets created in a known file system location?

Posted on 2012-03-21
4
433 Views
Last Modified: 2012-03-25
Hello,

I would like to write a script that will automatically unpack a file from a zipfile that gets created in a known file system location?  There is a new zipfile written out every day.  The underlying data is variable length (some records have more fields than others, but there are fields that are common to every record)text records that are delimited by a "|".  


For example, the day 1 file will be called: abc1.dat.zip
the day 2 file will be called:  abc2.dat.zip

The file could be written out to its own file OR appended to a master text file that includes all the unpacked files.  However, I WOULD NOT want each days file to overwrite the previous days file

The folder location is  "MYFOLDER"
The initial file name is "abc1.dat.zip" and will be incremented by 1 each day.

Thanks!

ps.  We are in a windows environment and eventually these files will be combined together and linked to Access 2007 OR loaded into microsoft access (appended to the ABC table (which will be all of the files appended together (abc1 + abc2 + abc3, etc.)
0
Comment
Question by:pls123
4 Comments
 
LVL 14

Expert Comment

by:Bill Ross
ID: 37752094
Hi,

What unzip processor are you using?

Bill
0
 

Author Comment

by:pls123
ID: 37752624
I am not sure.  Is there a native unzip processor for windows?  I would imagine that it would be that.
0
 
LVL 6

Assisted Solution

by:wshark83
wshark83 earned 200 total points
ID: 37752831
check this site has useful information and macros

unzip using default windows zup utility --> http://www.rondebruin.nl/windowsxpunzip.htm
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 300 total points
ID: 37753498
Hey,

You'll need a reference to Microsoft Office 1x.0 Object Library, and then the following code, adapted from the link that wshark43 supplied (which is for Excel) is tested and working

Sub UnzipTo(DefPath As String)
    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim fd As FileDialog
    
    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant
    
    'Use a With...End With block to reference the FileDialog object.
    With fd
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .Title = "Zip Selector"
        'Put a path in here and it will open to the right spot
        .InitialFileName = ""
        .ButtonName = "Unzip"
    
        If .Show = True Then
            'Destination folder
            'DefPath = "C:\Users\Ron\test\"    '<<< Change path
            If Right(DefPath, 1) <> "\" Then
                DefPath = DefPath & "\"
            End If
    
            FileNameFolder = DefPath
            vrtSelectedItem = .SelectedItems(1)
    
            '        'Delete all the files in the folder DefPath first if you want
            '        On Error Resume Next
            '        Kill DefPath & "*.*"
            '        On Error GoTo 0
    
            'Extract the files into the Destination folder
            Set oApp = CreateObject("Shell.Application")
            oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(vrtSelectedItem).Items
    
            MsgBox "You find the files here: " & FileNameFolder
    
            On Error Resume Next
            Set FSO = CreateObject("scripting.filesystemobject")
            FSO.DeleteFolder Environ("Temp") & "\Temporary Directory*", True
        End If
    End With
End Sub

Open in new window


Working sample attached
unzip.mdb
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This article will show, step by step, how to integrate R code into a R Sweave document
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now