Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find an XML somewhere in a zipfile or subzips

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello all

I have made a code that looks for a zip-file then take all the digits from the zipfile name and creates a folder with the name also unzip the content to the created folder.

now is my issue.
I need a loop/ a function that zip all the zipfiles inside a zip.

I have read some on google and forums but I haven't found solution yet.
Could someone help me.

this is my code so far:


Code:
Imports Microsoft.Office.Interop
Imports System
Imports System.IO
Imports System.IO.Compression

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Fpath = ("C:\XML_Files_Extract")
        Dim mystring As String = "C:\X\36001-37000\36001-36100\36062\"

        Dim arFiles As String()
        arFiles = Directory.GetFiles("C:\XML_Files_Extract\", "*original från kund*.zip")
        Dim i As Integer
        For i = 0 To arFiles.Length - 1
            Dim myZipandPath As String = (arFiles(i).ToString())
            Dim MyZipDigits = CInt(Val(New Text.StringBuilder((From ch In myZipandPath.ToCharArray Where IsNumeric(ch)).ToArray).ToString))
            Dim myDigitString As String = MyZipDigits ' converted array to string
            Dim ZPath As String = (Fpath + "\" + myDigitString)
            MkDir(ZPath)
            ZipFile.ExtractToDirectory(myZipandPath, ZPath)
        Next
    End Sub

could someone help me?

Thank you in advance
 
I won't be around now for the next couple of hours. When you've had chance to look at this let me know how you get on.
 
Thank you man awesome!! I'll try it out then!
 
Corrections to my previous post.

I've created some folders to make it clearer:
C:\TTTesting
C:\TTTesting\ElmnasTestFolder
C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles
C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles

I placed a Zip file in C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles so that I could test everything and to avoid the confusion my earlier post probably caused I've used these folders in the code - so that all you will need to do is change them to your requirements.

The Module:

Code:
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Imports System.Text.RegularExpressions

Module ElmnasModule


	'Pattern
	Private GetNumbersPattern As String = "[^\d]"

	Public Function GetNumbers(Source As String) As String

		Return New Regex(GetNumbersPattern).Replace(Source, "")

	End Function


	Public Function ExtractFiles(SourceFile As String, TargetFolder As String, BaseFolder As String) As Boolean

		'This makes sure that the folder parts each end with "\"
		ZipFile.ExtractToDirectory(SourceFile, BaseFolder & If(BaseFolder.EndsWith("\"), "", "\") & TargetFolder & If(TargetFolder.EndsWith("\"), "", "\"))
		Return True

	End Function


End Module

Sample code - the Form contains two Buttons and a ListBox:

Code:
Imports System.IO

Public Class Form2

	Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click

		Close()

	End Sub



	Private Sub RunButton_Click(sender As Object, e As EventArgs) Handles RunButton.Click

		RunButton.Enabled = False

		'Assign these two folders as appropriate

		'Dim SourceFolder As String '= The Name Of The Folder That Contains The Zipped Files
		Dim SourceFolder As String = "C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles"

		'Dim TargetParentFolder As String '= The Name Of The Folder That Is The Parent Of The Extraction Folder
		Dim TargetParentFolder As String = "C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles"


		For Each fn As String In Directory.GetFiles(SourceFolder, "*.zip")
			ElmnasModule.ExtractFiles(fn, ElmnasModule.GetNumbers(fn), TargetParentFolder)
		Next

		'As an example - display the list the extracted files in a list box
		ListBox1.Items.Clear()

		For Each fn As String In Directory.GetFiles(SourceFolder, "*.zip")
			For Each f As String In Directory.GetFiles("C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles\" & ElmnasModule.GetNumbers(fn), "*.*")

				'"f" will be the name of each file that was extracted from the above code
				'Here you can process each of the extracted files as you require

				ListBox1.Items.Add(f)

			Next
		Next

		RunButton.Enabled = True

	End Sub

End Class

 
Thank you sir I will take a look on your master code when I am finished with this :)

Awesome!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top