PDA

View Full Version : [SOLVED] Help Understanding Code



Aze98
05-31-2016, 09:28 AM
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?:banghead:

Paul_Hossler
05-31-2016, 09:43 AM
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

snb
05-31-2016, 10:02 AM
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

Aze98
05-31-2016, 10:32 AM
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?

Paul_Hossler
05-31-2016, 11:10 AM
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

SamT
05-31-2016, 01:49 PM
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.