Developing Matt

My Technical Journal

Unzip and the copyHere command (with vUseless vOptions)

with 7 comments

If you want to copy a file from one location to another you can certainly do it with using a folder shell object. A bonus: using the shell object enables you to copy from a zipped location. this is great until you need to use it as a hidden process requiring no user input. The problem is that if the file you are unzipping already exists you’ll get a prompt asking if you want to overwrite. The solution that doesn’t work: the shell32.folder.copyhere(sourcefolder, vOptions) has a vOptions which enables you to answer ‘yes to all’ for all overwrite prompts. Let me save you some time: contrary to the msdn article, it doesn’t work. My solution: create an unzip application and call it from another application as a process…setting the visible property to false.

Unzip vb script:

 

Dim sourcefile As String = Command$()
If Right(sourcefile, 1) = “\” Then sourcefile = Left(sourcefile, Len(sourcefile) – 1)
Dim sPath As String = Left(sourcefile, InStrRev(sourcefile, “\”))

Dim myShell As New Shell32.Shell
Dim sourceFolder As Shell32.Folder = myShell.NameSpace(sourcefile)
Dim destinationFolder As Shell32.Folder = myShell.NameSpace(sPath)
Try
Dim i As Integer
For i = 0 To sourceFolder.Items.Count – 1
If System.IO.File.Exists(destinationFolder.Items.Item.Path & “\” & sourceFolder.Items.Item(i).Name) Then _
System.IO.File.Delete(destinationFolder.Items.Item.Path & “\” & sourceFolder.Items.Item(i).Name)
destinationFolder.CopyHere(sourceFolder.Items.Item(i))
Console.WriteLine(“Unzipped ” & sourceFolder.Items.Item(i).Name & ” to ” & destinationFolder.Items.Item.Path)
Next
Return 0
Catch ex As Exception
Console.WriteLine(“Could not unzip from ” & sourcefile & ” to ” & destinationFolder.Items.Item.Path)
Console.WriteLine(“Error: ” & Err.Description)
Return Err.Number
Finally
destinationFolder = Nothing
sourceFolder = Nothing
myShell = Nothing
End Try

After you create this exe you can call it as a process:

 

Dim iProcess As New System.Diagnostics.ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + “unzip.exe”)

iProcess.CreateNoWindow = True
Dim sArgs As String = ZippedFile
iProcess.Arguments = sArgs
iProcess.WindowStyle = ProcessWindowStyle.Hidden
Dim p As New System.Diagnostics.Process
iProcess.UseShellExecute = False
p = System.Diagnostics.Process.Start(iProcess)
p.WaitForExit(30000)
Dim s As Integer = p.ExitCode
iProcess.UseShellExecute = True

p.Dispose()
iProcess = Nothing

Written by matt

August 8, 2007 at 2:28 pm

Posted in VB.net

7 Responses

Subscribe to comments with RSS.

  1. This is a old post, but ill post the solution for whomever maybe have the same problems.

    The garbage collector of this function does not work. The variable passed pertains to where you are attempting to copy to, not the functions temporary scratch space. The first time you use this function, it will work, but if you try to run it multiple times from the same zip file, you will receive “the file exists” error because the scratch space already contains a file of that name. It gets confused and errors with “the file exists”

    Go to “c:\documents and settings\username\localsettings\temp\temporary directory xx for whatever.zip\”

    Delete all of the “temporary directory for” directories and run it again and it will function.

    aasdf

    October 9, 2008 at 11:37 am

  2. great information. thank you for posting.

    matt

    October 9, 2008 at 2:29 pm

  3. Thanks – I execute CopyHere in a powershell script, which stopped working mysteriously due to this error.

    Doug

    February 16, 2009 at 10:54 am

  4. Thanks alot – I have been having this problem for days and still pulling my hair out until I see this post. What a lifesaver. Thanks again.

    andy

    April 14, 2009 at 9:42 pm

  5. Thanks for this wonderful post…
    Great Job!

    Johnjames

    May 18, 2009 at 3:15 am

  6. Thanks!!! My hair turned grey in my trial-and-error’s to solve this problem. You really made my day!

    HÃ¥kan from Sweden

    June 30, 2009 at 11:16 am

  7. Thanks so much for the information! I wasn’t able to solve the problem by clearing out the “temp” directories, but from what you explained, I assumed if I changed the name of the zip folder then it might solve the issue – which it did! Thanks again!!!!

    Kristin

    September 10, 2009 at 6:31 am


Leave a Reply