Answered by:
Split files into sub folders based on file name

Question
-
User1717218719 posted
Hi,
I have a zip file which contains various zip files in it. I am looking to move files to a new folder in my directory based on files name (first thre letters of the file = "abc" for example).
So far I have wrote code for a file uploader and code to create a new folder in the directory I want.
I am now looking to get the various file names within my upload file.
Example:
test.zip containes:
- abc.zip
- xyz.zip
- 123.zip
I want to be able to find "abc.zip" and move it to the abc folder in my new directory I have previously created.
I am struggling to find how to get the list of files in my test.zip. If anyone has any suggestions or can help at all that would be great.
Here is some of the the code I have wrote so fare to give a clearer picture.
Protected Sub BtnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click Dim FileUpload1 As FileUpload = TryCast(FindControl("FileUpload1"), FileUpload) If FileUpload1.HasFile And FileUpload1.FileContent IsNot Nothing Then Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName) If extension = ".zip" Then Select Case extension Case ".zip" ' Call Filename() '--Creates Directory Call Main() Exit Select End Select Else '--ERROR MsgBox("File not in the format .zip") End If End If End Sub '--CREATES NEW DIRECTORY Public Sub Main() ' Specify the directory you want to manipulate. Dim path As String = "G:\MyNewDir" Try ' Determine whether the directory exists. If Directory.Exists(path) Then Console.WriteLine("That path exists already.") Return End If ' Try to create the directory. Dim di As DirectoryInfo = Directory.CreateDirectory(path) Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path)) Catch e As Exception Console.WriteLine("The process failed: {0}.", e.ToString()) End Try End Sub '--READ FILE Name Public Sub Filename() If (FileUpload1.HasFile) Then '--GET LIST OF FILES IN FILEUPLOAD Else End If End Sub '-- MOVE FILE TO DIRECTORY
<!DOCTYPE html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Upload File</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload File" /> <br /> <br /> <asp:Label ID="LblErr" runat="server" style="color:red"></asp:Label> </div> </form> </body> </html>
Monday, June 15, 2020 3:29 PM
Answers
-
User-943250815 posted
You can use DotNetZip available on Nuget.
You can get instructions and sample on GitHub https://github.com/haf/DotNetZip.Semverd
Here a sample of how to get file name before extractImports Ionic.Zip
Dim path As String = "G:\MyNewDir" Using MyZipFile As ZipFile = ZipFile.Read([use zip filename here]) For Each e As ZipEntry In MyZipFile Dim MyFilename As String = e.FileName ' Get file name
if MyFilename.contains("abc") then e.Extract(path, ExtractExistingFileAction.DoNotOverwrite) ' Extract file to disk
End If Next End Using- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 15, 2020 7:22 PM -
User-939850651 posted
Hi, E.RU
Based on your description, I created a demo and get file list using ZipArchive.
More details, please refer below code:
Public Shared Sub Main() Dim path As String = "D:/Test.zip" Using archive As ZipArchive = ZipFile.OpenRead(path) For Each entry As ZipArchiveEntry In archive.Entries Console.WriteLine(entry.Name) If entry.Name.Contains("abc") Then 'copy file to destination End If Next Console.ReadLine() End Using End Sub
Result:
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 16, 2020 10:28 AM
All replies
-
User-943250815 posted
You can use DotNetZip available on Nuget.
You can get instructions and sample on GitHub https://github.com/haf/DotNetZip.Semverd
Here a sample of how to get file name before extractImports Ionic.Zip
Dim path As String = "G:\MyNewDir" Using MyZipFile As ZipFile = ZipFile.Read([use zip filename here]) For Each e As ZipEntry In MyZipFile Dim MyFilename As String = e.FileName ' Get file name
if MyFilename.contains("abc") then e.Extract(path, ExtractExistingFileAction.DoNotOverwrite) ' Extract file to disk
End If Next End Using- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 15, 2020 7:22 PM -
User-939850651 posted
Hi, E.RU
Based on your description, I created a demo and get file list using ZipArchive.
More details, please refer below code:
Public Shared Sub Main() Dim path As String = "D:/Test.zip" Using archive As ZipArchive = ZipFile.OpenRead(path) For Each entry As ZipArchiveEntry In archive.Entries Console.WriteLine(entry.Name) If entry.Name.Contains("abc") Then 'copy file to destination End If Next Console.ReadLine() End Using End Sub
Result:
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 16, 2020 10:28 AM -
User1717218719 posted
Thank you both.
These answers were a great help and I now have my code working exactly how I want
Thanks :)
Tuesday, June 16, 2020 10:46 AM