PDA

View Full Version : Dont Change Folder/Zip File after Zipping & Unzipping



deedii
05-28-2012, 10:15 PM
Hi people Im using the code of Ron de Bruin of zipping and unzipping file. Now my problem is it generates new file name I want to have the same filename of the zip file i unzip and the same filename of the folder i want to zip.

UNZIP


Option Private Module
Sub UnZipFile()
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 = "W:\"
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



ZIP


Option Private Module
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
Sub ZipFile()
Dim strDate As String, DefPath As String, sFName As String
Dim oApp As Object, iCtr As Long, I As Integer
Dim fName, vArr, FileNameZip
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"
'Browse to the file(s), use the Ctrl key to select more files
fName = Application.GetOpenFilename(filefilter:="Txt Files (*.txt*), *.txt*", _
MultiSelect:=True, Title:="Select the files you want to zip")
If IsArray(fName) = False Then
'do nothing
Else
'Create empty Zip File
NewZip (FileNameZip)
Set oApp = CreateObject("Shell.Application")
I = 0
For iCtr = LBound(fName) To UBound(fName)
vArr = Split97(fName(iCtr), "\")
sFName = vArr(UBound(vArr))
If bIsBookOpen(sFName) Then
MsgBox "You can't zip a file that is open!" & vbLf & _
"Please close it and try again: " & fName(iCtr)
Else
'Copy the file to the compressed folder
I = I + 1
oApp.Namespace(FileNameZip).CopyHere fName(iCtr)
'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = I
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
End If
Next iCtr
MsgBox "You find the zipfile here: " & FileNameZip
End If
End Sub

defcon_3
05-29-2012, 01:52 AM
Im confused??
You mean for example we have a zip file called "deedii.zip", after unzipping you want to have a folder name "deedii" containing the unzip files? And if we zip it again the result would be "deedii.zip" again? Is that how you want it to be?

Just change the ff;
On your code the output will be;

Unzip

'Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss") ' you dont need this
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\" 'Change this


Zip


FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip" 'Change this as well

deedii
05-29-2012, 01:56 AM
Yes exactly. The problem is I dont know the syntax or the changes needed to achieve that. A lil help?

CatDaddy
05-29-2012, 09:11 AM
remove & strDate from both lines

deedii
05-29-2012, 06:09 PM
I dont think so coz
"MyFilesZip "

Will be the file name. It should be the folder name itself or the zip filename.

GTO
05-29-2012, 06:34 PM
Hi Deedii,

Could you include a link to the page(s) at Ron DeBruin's website where you copied the code from?

Thank you,

Mark

deedii
05-29-2012, 07:38 PM
Hi GTO,

Here: http://www.rondebruin.nl/windowsxpzip.htm

deedii
05-30-2012, 12:48 AM
Anybody can help me?

Bob Phillips
05-30-2012, 01:27 AM
Sorry, can you explain again in simple terms (without any code) what you are trying to do?

deedii
05-30-2012, 01:44 AM
Example:

Zipping
I want to zip a folder name "deedii" the output name should be "deedii.zip"

Unzipping
I want to unzip "deedii.zip" the output will be a folder name "deedii"

With that code here http://www.rondebruin.nl/windowsxpzip.htm it generates new filename which is MyZipFile + Date.zip and MyUnzipFolder, It should be the foldername I want to zip and unzip.

deedii
05-30-2012, 07:24 AM
anyone guys? pretty please :(