|
hmmm try option 16
output.CopyHere(input.Items, 16)
anyway.. if it didn't work you can make your own checking code.
If IO.File.Exists("file_path") = True Then
IO.File.Delete("file_path")
Else
End If
Best Regards
|
|
|
|
|
putting the 16 didnt work so i will try your other option, thank you
|
|
|
|
|
Seems none of them worked
|
|
|
|
|
dude the 2nd method should work.
I think u just didn't put it in the right place .
it should be like this :
Assume we want to create a file "d:\hello.zip" and we have a button "Button1 " to do that action.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IO.File.Exists("d:\hello.zip") = True Then
IO.File.Delete("d:\hello.zip")
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes("d:\hello.zip", startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace("D:\neededFiles")
Dim output As Shell32.Folder = sc.NameSpace("d:\hello.zip")
output.CopyHere(input.Items, 4)
Else
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes("d:\hello.zip", startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace("D:\neededFiles")
Dim output As Shell32.Folder = sc.NameSpace("d:\hello.zip")
output.CopyHere(input.Items, 4)
End If
End Sub
That's it!
It's simple.
Best Regards
|
|
|
|
|
Code it simple without wasting IF THEN ELSE and repeating same code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IO.File.Exists("D:\hello.zip") Then IO.File.Delete("D:\hello.zip")
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes("D:\hello.zip", startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace("D:\neededFiles")
Dim output As Shell32.Folder = sc.NameSpace("D:\hello.zip")
output.CopyHere(input.Items, 4)
End Sub
|
|
|
|
|
hai gerald_b,
Thanks for your both replies. I know that it could be done like that, in both of your replies, but when someone asks about something i like to explain it logically and step by step, i didn't give him the code to copy and paste it.
I know it's inefficient, i just gave him the idea, he's free to implement it the way he likes, but thank you for pointing the efficient ways, it would be helpful for people reading this topic.
Best Regards
|
|
|
|
|
can you possibly make this overwrite existing file to ?
if yes p me
|
|
|
|
|
Hello,
I'm really sorry for this late reply it should send me a notification when someone comments on the article but it did not ......
Do you mean overwriting an already made zip file ?
As i remember it automatically overwrites the output file ?
More details would be appreciated!
Best Regards
|
|
|
|
|
The compress folder creation part works fine. But i have a issue that once its starts a process of compression it tries to execute VB code, for which i want it stop till the compression is completed. I tried with Thread sleep but u cannot judge the file size as it varies time to time. Hope i have made my point clear. Any Help would be appreciated. My mail id : pankaj1307@gmail.com. If you found a solution
|
|
|
|
|
Last question I promise!
Originally I was just putting the code for the sub in my main code-
Dim Username As String = Environment.UserName
Dim Downloads As String = "C:\Users\" & Username & "\Downloads"
Dim AppData As String = "C:\Users\" & Username & "\AppData\Roaming"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Value Strings
Dim MD As String = Downloads & "\test.zip"
Dim AD As String = AppData & "\testdirectory"
'Unzip test.zip into folder
Dim sc As New Shell32.Shell()
IO.Directory.CreateDirectory(AD)
Dim output As Shell32.Folder = sc.NameSpace(AD)
Dim input As Shell32.Folder = sc.NameSpace(MD)
output.CopyHere(input.Items, 16)
'Re-value Strings
MD = AppData & "\HMMI\test.2.zip"
AD = AppData & "\HMMI\test"
output = sc.NameSpace(AD)
input = sc.NameSpace(MD)
'Unzip test2.zip into folder
output.CopyHere(input.Items, 16)
End Sub
But I only did this out of ignorance on how to actually call the sub into my main command.
I know how to create it, but what line would I use to call the sub? And, can I call it multiple times with new information or do I need to re-value the strings as I did prior?
|
|
|
|
|
hai ,
I hope i got you right.
the sub you need to create is this
Sub unZIP(ByVal inputZip As String, ByVal outputFolder As String)
Dim sc As New Shell32.Shell()
IO.Directory.CreateDirectory(outputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputFolder)
Dim input As Shell32.Folder = sc.NameSpace(inputZip)
output.CopyHere(input.Items, 16)
End Sub
So when you want to unzip a file you simply provide input zip file and output folder :
unZIP(Downloads & "\test.zip",AppData & "\testdirectory")
So the code for unzipping the two of your files is :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
unZIP(Downloads & "\test.zip",AppData & "\testdirectory")
unZIP(AppData & "\HMMI\test.2.zip",AppData & "\HMMI\test")
End Sub
If this is not what you are asking for then please inform me.
Best Regards
|
|
|
|
|
This is exactly what I was looking for! But how do I call the Zip() sub? Apparently it isn't the same as the unZIP sub.
modified 19-Nov-12 11:53am.
|
|
|
|
|
the zipping sub should be :
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
End Sub
Calling it is just as simple as calling the unzip() sub :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Zip("D:\myfiles", "D:\myZip.zip")
End Sub
|
|
|
|
|
Since this runs in a separate process, but is still under the same process name, I can't figure out how to make my program wait until it is finished compressing.
I thought maybe I could have it loop until the compressed file is made. But with this code, it makes the file, then fills it. So I am completely lost..
|
|
|
|
|
Hello ,
Don't worry it's simple.
To check if the file is being used by another program you could open it into a stream and check.
If it will throw an error this means it's being used by another process ( in our program shell32 ) otherwise shell32 has finished compressing and the file is ready to use.
So use this code to check if file being used by another program
Function isFileready(path As String) As Boolean
Try
Dim j As IO.Stream = IO.File.Open(path, IO.FileMode.Open)
j.Close()
Return (True)
Catch ex As Exception
Return (False)
End Try
End Function
So the final code for compressing a file and waiting for it until it finishes compressing :
Private Sub Compress()
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes("d:\test.zip", startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace("D:\appdata\HMMI\test")
Dim output As Shell32.Folder = sc.NameSpace("D:\test.zip")
output.CopyHere(input.Items, 16)
Do Until FileLen("D:\test.zip") > 22
Loop
Do Until isFileready("D:\test.zip") = True
Loop
Me.Text = "done!"
End Sub
Function isFileready(path As String) As Boolean
Try
Dim j As IO.Stream = IO.File.Open(path, IO.FileMode.Create)
j.Close()
Return (True)
Catch ex As Exception
Return (False)
End Try
End Function
Best Regards
|
|
|
|
|
You are my hero. Thank you. I will implement this today.
|
|
|
|
|
|
It is telling me that the "FileLen" cannot find my file "test.zip"
Sub unZIP(ByVal inputZip As String, ByVal outputFolder As String)
Dim sc As New Shell32.Shell()
IO.Directory.CreateDirectory(outputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputFolder)
Dim input As Shell32.Folder = sc.NameSpace(inputZip)
output.CopyHere(input.Items, 16)
End Sub
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
End Sub
...
'Compress testdirectory to test.zip
Zip(AppData & "\HMMI\testdirectory", AppData & "\HMMI\test.zip")
'Wait for compression to finish
Do Until FileLen(AppData & "\HMMI\test.zip") > 22
Loop
Do Until isFileready(AppData & "\HMMI\test.zip") = True
Loop
Anything else I make can see it perfectly, and I've physically checked to make sure it is there. Not sure why it can't see it?
|
|
|
|
|
hmmm weird
Ok try putting this line before the Filelen function to make sure the file has been created :
Do Until FileIO.FileSystem.FileExists(AppData & "\HMMI\test.zip") = True
Loop
i mean it should be like this :
Do Until FileIO.FileSystem.FileExists(AppData & "\HMMI\test.zip") = True
Loop
Do Until FileLen(AppData & "\HMMI\test.zip") > 22
Loop
Do Until isFileready(AppData & "\HMMI\test.zip") = True
Loop
|
|
|
|
|
Wow, I'm really amazed that didn't work. Still says FileLen cannot find it. I mean, the "file exists" loop proves that it is actually there or it wouldn't even get to the FileLen command.
modified 19-Nov-12 16:42pm.
|
|
|
|
|
aww
maybe your application doesn't have permissions to access that file ? i mean the directory that you are doing your operation in it might require administrator permissions.
So to make sure everthing is OK , try the program with another file that's in a directory that doesn't need permission , let's say like the D:\
try it
|
|
|
|
|
I've tried that, and a ton of other ways. But I think I've got an idea. Is there a way that I can make Sub Zip create a file/directory without interfering with the Sub itself?
Something like -
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
System.IO.Directory.CreateDirectory(Appdata & "\testdirectory")
End Sub
This would have worked but the "CreateDirectory" line messed with the Sub Zip.
|
|
|
|
|
ehm .. put this in the beginning of the sub
If IO.Directory.Exists(Appdata & "\testdirectory") = False Then
System.IO.Directory.CreateDirectory(Appdata & "\testdirectory")
End If
|
|
|
|
|
|
oh no i never get desperate.
The thing is that when u first asked about it, i did exactly what you have asked for and it worked on my pc =/ .... i don't know why it's not working on yours ..
hey how about sending me your project ?
I'm a good debugger i promise nothing of your project files will be used here or there so you are safe.
If you feel OK about that then send it to me : bashar.tahir@yahoo.com
I will debug it from the beginning, test everything and fix the errors.
Best Regards
|
|
|
|
|