Consulting

Results 1 to 6 of 6

Thread: Help Understanding Code

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    2
    Location

    Help Understanding Code

    Hello,

    I am trying to understand the final 2 lines in this code after "On Error Resume Next."

    Sub Unzip1()    Dim FSO As Object
        Dim oApp As Object
        Dim Fname As Variant
        Dim FileNameFolder As Variant
        Dim DefPath As String
        Dim strDate As String
    
    
        Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                            MultiSelect:=False)
        If Fname = False Then
            'Do nothing
        Else
            'Root folder for the new folder.
            'You can also use DefPath = "C:\Users\Ron\test\"
            DefPath = Application.DefaultFilePath
            If Right(DefPath, 1) <> "\" Then
                DefPath = DefPath & "\"
            End If
    
    
            'Create the folder name
            strDate = Format(Now, " dd-mm-yy h-mm-ss")
            FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
    
    
            'Make the normal folder in DefPath
            MkDir FileNameFolder
    
    
            'Extract the files into the newly created folder
            Set oApp = CreateObject("Shell.Application")
    
    
            oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
    
    
            'If you want to extract only one file you can use this:
            'oApp.Namespace(FileNameFolder).CopyHere _
             'oApp.Namespace(Fname).items.Item("test.txt")
    
    
            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 Sub
    Reference: rondebruin

    The code basically unzips files and places it in a directory you specify (FileNameFolder). But I don't get what the "FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True" is doing.

    What folder is it deleting? After executing the code, I never see a temporary directory created in my "Temp" directory so why is it trying to delete this?


    Anyone have any ideas?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
            On Error Resume Next 
            Set FSO = CreateObject("scripting.filesystemobject") 
            FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    "On Error Resume Next" just means that if an error occurs on any following statement, ignore it and keep trying to execute statements, until error checking is turned on with a On Error Goto 0

    In this case, if there are no folders in (probably) "c:\Users\Aze98\AppData\Local\Temp" that look like "\Temporary Directory*" to delete, ignore the error (I.e. don't show the dialog) that would normally be generated by trying to delete non-existent folders, and just continue to the End

    If there are folders in "c:\Users\Aze98\AppData\Local\Temp" that look like "\Temporary Directory*" then delete them and continue to the End
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        With Application.FileDialog(1)
            .InitialFileName = "*.zip"
            If .Show Then
               c00 = .SelectedItems(1)
               With CreateObject("Shell.Application")
                   .Namespace(Application.DefaultFilePath).CopyHere .Namespace(c00).items
               End With
            End If
        End With
    End Sub
    If this errors out, use
    Sub M_snb() 
        With Application.FileDialog(1) 
            .InitialFileName = "*.zip" 
            If .Show Then 
                c00 = .SelectedItems(1) 
                With CreateObject("Shell.Application") 
                    .Namespace(Application.DefaultFilePath & "\").CopyHere .Namespace(c00).items 
                End With 
            End If 
        End With 
    End Sub

  4. #4
    VBAX Newbie
    Joined
    May 2016
    Posts
    2
    Location
    Quote Originally Posted by Paul_Hossler View Post
            On Error Resume Next 
            Set FSO = CreateObject("scripting.filesystemobject") 
            FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    "On Error Resume Next" just means that if an error occurs on any following statement, ignore it and keep trying to execute statements, until error checking is turned on with a On Error Goto 0

    In this case, if there are no folders in (probably) "c:\Users\Aze98\AppData\Local\Temp" that look like "\Temporary Directory*" to delete, ignore the error (I.e. don't show the dialog) that would normally be generated by trying to delete non-existent folders, and just continue to the End

    If there are folders in "c:\Users\Aze98\AppData\Local\Temp" that look like "\Temporary Directory*" then delete them and continue to the End
    Thank you for the reply.

    In the code, do you see why this would be needed anyway? I don't see any reason to check that temp folder? We are never copying to that folder. Am I missing something?

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Off hand, and based on the

    DefPath = Application.DefaultFilePath

    to start with, it doesn't seem to be needed


    BTW, the last time I looked at something like Ron's code, the extraction was to something like

    Environ ("Temp") & "\" & "Temporary Directory-20160531"

    so they might just some leftover lines
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Nothing in the previous code creates anything named "Temporary Directory", so those three lines should be deleted in case any other program or a user should make and need such a folder.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •