Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
450 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 800 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 1200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

885 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