Unzip files that begin with a particular string

458 Views Asked by At

This is my second attempt at cracking this problem I am facing.

In a nutshell I get some files sent to me on a weekly basis which begin with the same 11 characters and anything after that can differ from week to week.

I have some basic code that unzips the files when an exact filename is found, but I'm hoping to move away from that by having a procedure which uses wildcards. So anything that is a zip file and anything that begins with those 11 chars.

I have gotten this far, but seem to have hit a wall with a name space error, can somebody please help me out where I'm going wrong?

Set fso = CreateObject("Scripting.FileSystemObject")

ExtractTo="C:\Users\W1 Process\_ThisWeek\"

For Each f In fso.GetFolder("C:\Users\W1 Process\_ThisWeek\").Files
  If LCase(fso.GetExtensionName(f)) = "zip" And Left(f.Name, 11) = "Home Weekly" Then
    set objShell = CreateObject("Shell.Application")

    set FilesInZip=objShell.NameSpace(ZipFile).items
    objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)

    Set fso = Nothing
    Set objShell = Nothing
  End If
Next
1

There are 1 best solutions below

2
On BEST ANSWER

You could use a regular expression for matching variable file names:

Set re = New RegExp
re.Pattern = "^home weekly.*\.zip$"

Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")

ExtractTo = "C:\Users\W1 Process\_ThisWeek"

For Each f In fso.GetFolder("C:\Users\W1 Process\_ThisWeek").Files
  If re.Test(f.Name) Then
    Set FilesInZip = app.NameSpace(f.Path).Items
    app.NameSpace(ExtractTo).CopyHere(FilesInZip)
  End If
Next