Hello so I have searched for this a long time allready and havent found anything about this. I search all the folders in the given folder and create an entry in the ziparchive for each file. It is needed that i keep the fodler structure. This is what i have come up with for now.
Imports System.IO
Imports System.IO.Compression
'a list of folders I want to zip. These are all located in the testfolder.
Public Shared ListofBackupFolders As New List(Of String) From {"SDK", "Programms", "Application", "Office", "Reports", "UserSettings", "de", "Intrastat", "XSD"}
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim d As New DirectoryInfo("c:\temp\testfolder")
Dim directories = d.GetDirectories("*")
File.Create("C:\temp\test.zip").Close()
For Each directory In directories
'Only searches the Folders I selecten in the list
If ListofBackupFolders.Contains(directory.Name) Then
SearchFolder(directory.FullName)
End If
Next
End Sub
Private Sub SearchFolder(path As String)
Dim dirInfo As New DirectoryInfo(path)
Dim subDirectories = dirInfo.GetDirectories("*")
If path.EndsWith("Application") Then
'>>> Some of the selected folders are in the Application folder
For Each subdirectory In subDirectories
If ListofBackupFolders.Contains(subdirectory.Name) Then
'recursion to get all the folders in the selected folder
SearchFolder(subdirectory.FullName)
End If
Next
Else
If subDirectories.Any Then
For Each subdirectory In subDirectories
'>>> recursion to get all the folders in the selected folder
SearchFolder(subdirectory.FullName)
Next
End If
End If
Dim files = dirInfo.GetFiles("*")
Using zipToOpen As FileStream = New FileStream("C:\temp\test.zip", FileMode.Open)
Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
Dim readmeEntry As ZipArchiveEntry
For Each file In files
'for every File in the folders it creates the path in the zip
Dim zipPath = file.FullName.Replace("C:\", "").Replace("\", "/")
readmeEntry = archive.CreateEntryFromFile(file.FullName, zipPath, CompressionLevel.Fastest)
Next
If Not subDirectories.Any AndAlso Not files.Any Then
'if there are no files in a folder i still want the folder in the zip
Dim folderPath = path.Replace("C:\", "").Replace("\", "/") & "/"
readmeEntry = archive.CreateEntry(folderPath)
End If
End Using
End Using
End Sub
Now the problem I have is that i havent found a way to split the zip after a given volume (example 200mb).
And a other thing I need is that i have a single file that is for example 2gb i have to split that one aswell. This is seperate from the other code.
Related
I am working on a program that will move files to a database, text files to be exact. The user will have a starting directory and inside will multiple sub folders and files and so on. I want to go through each Folder and sub folder looking for the text files and add them accordingly to the database so it resembles a directory structure in a way. In the program the files are organized such as the folders are "Categories" that are displayed into a tree view.I am only adding the folder names(as Categories) that do contain text files and their subs and so forth. As well I need to figure out where to plug in the adding of the "Category". As of right now I am using a couple of listboxes for my output until I can get it all figured out.
lstfiles.Items.Add(Dir)
For Each file In System.IO.Directory.GetFiles(Dir)
Dim fi As System.IO.FileInfo = New IO.FileInfo(file)
If fi.Extension = ".txt" Then
If lstRootFolderFiles.Items.Contains(file) = False Then
lstfiles.Items.Add(file)
AddFile(file, Gennumber(9999))
End If
End If
Next
For Each folder In System.IO.Directory.GetDirectories(Dir)
lstfiles.Items.Add(folder)
For Each file In System.IO.Directory.GetFiles(folder)
Dim fi As System.IO.FileInfo = New IO.FileInfo(file)
If fi.Extension = ".txt" Then
If lstRootFolderFiles.Items.Contains(file) = False Then
lstfiles.Items.Add(file)
End If
End If
Next
Next
I have gotten so far as to iterate through the directory and get files but it returns folders that are empty. And I need to figure out where I need to put in my addcategory function. And also remember the last one that was added so they can be added as a subcategory.
I think I am making a big mess of it all, or over thinking the whole thing.
Any assistance or guidance would be appreciated.Thank you
The end result that I came up with was much different from my original posting, as it was my way of thinking of how it was going to work. I split up the two main functions. Retrieving the Folders and retrieving the files.
DirEx is the Import Directory, CatID is the Tag of the selected Treenode where the folders are going to added in.
Private Sub DirImport_GetSubDirectories(ByVal DirEx As String, ByVal CatID As Integer)
Try
Dim ClsData As New clsNoteData
'Set the DatabaseFile Property of the class
ClsData.Database = LoadedLibraryDatabase
' Get all subdirectories
Dim subdirectoryEntries() As String = Directory.GetDirectories(DirEx)
' Loop through them to see if they have any other subdirectories
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
'If the Directory is not Empty
If Not Directory.GetFileSystemEntries(subdirectory).Length = 0 Then
Dim di As DirectoryInfo = New DirectoryInfo(subdirectory)
'Creating Random Number
Dim NewCatID As Integer = GenNumber(9999)
'Call for a new Sub Category
ClsData.NewSubCategoryNode(LoadedLibraryDatabase, NewCatID, CatID, di.Name, -1)
'Get files in the current Directory
DirImport_GetFiles(subdirectory, NewCatID)
'Get the next set of Subfolders
DirImport_GetSubDirectories(subdirectory, NewCatID)
End If
Next
Catch ex As Exception
End Try
End Sub
Private Sub DirImport_GetFiles(ByVal DirEx As String, ByVal CatID As Integer)
Dim Files() As String = Directory.GetFiles(DirEx, "*.txt")
Dim file As String
For Each file In Files
Dim clsNoteData As New clsNoteData
Dim fi As FileInfo = New FileInfo(file)
clsNoteData.Database = LoadedLibraryDatabase
clsNoteData.NewNote_ID = GenNumber(99999)
clsNoteData.NewNote_CatID = CatID
clsNoteData.NewNote_Title = Path.GetFileNameWithoutExtension(file)
clsNoteData.NewNote(False, file)
Next
End sub
So here it is for anyone who may want to do something similar.
I am learning VB.NET and writing a utility to copy only the modified files from a preset selection of folders to their equivalent in a backup directory.
For example: D:\Profiles\Mail to E:\Backup\Profiles\Mail
I can already do the copy simply with the following:
For Each item In MAILp
My.Computer.FileSystem.CopyDirectory(MAILp, MAILd, True)
Next
But the idea is count the amount of modified files to be copied, and display this all through a progress bar and the background worker (eventually)
At the moment i am working with this;
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Contants for whole activity
Dim lstFiles As New List(Of FileInfo)
Dim fsInfo As FileSystemInfo
'Constants for dirInfo's
Dim dirInfoMAILp As New DirectoryInfo(MAILp)
Dim dirInfoDESKTOPp As New DirectoryInfo(DESKTOPp)
'Loop for Mail
For Each fsInfo In dirInfoMAILp.GetFileSystemInfos
Dim strDestFileName As String = Path.Combine(MAILd, fsInfo.Name)
Dim destFileInfo As New FileInfo(strDestFileName)
If fsInfo.LastWriteTime > destFileInfo.LastWriteTime Then
lstFiles.Add(fsInfo)
End If
Next
'Loop for Desktop
For Each fsInfo In dirInfoDESKTOPp.GetFileSystemInfos
Dim strDestFileName As String = Path.Combine(DESKTOPd, fsInfo.Name)
Dim destFileInfo As New FileInfo(strDestFileName)
If fsInfo.LastWriteTime > destFileInfo.LastWriteTime Then
lstFiles.Add(fsInfo)
End If
Next
'Number of files to copy
Label1.Text = lstFiles.Count
For Each file As FileInfo In lstFiles
System.IO.File.Copy(file.FullName, DESTINATIONMAIN + file.Name, True)
Next
End Sub
My problem is in three parts:
1. If the files do not already exist in the destination, it will throw and exception. This is not good if the utility is being used for the first time. How can I use an ELSE in the loop to copy the file if it doesn't exist in the destination?
2. The copy does not copy folders, only files, can I expand it to include folders? Ideally the differential check loop targets the Top folder then goes through every sub folder and file, adding it to the list.
3. When copied to the destination folder, the files are copied to the destination top folder "E:\Backup\Profiles", rather than in the sub-folders they were copied from.
Can anyone point me in the right direction?
I am trying to get my application to check for folders in the folderbrowserdialogs selectedpath and then get those files, but it doesn't work I have tried both listed ways below. The second way gives me an error: (Expression is of type char which is not a collection type)
For Each folder In FileBrowserDialog.SelectedPath
Dim counter As _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter = My.Computer.FileSystem.GetFiles(folder)
Label1.Text = counter.Count.ToString
Next
For Each folder In FileBrowserDialog.SelectedPath
Dim counter As _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)
For Each foundfile In folder
counter = My.Computer.FileSystem.GetFiles(foundfile)
Label1.Text = counter.Count.ToString
Next
Any help is appreciated.
FolderBrowserDialog1.SelectedPath will return the path the user selected in the dialog. You still need to write code to go get the files. There may not be a need to get the folders and then files in them. Net has ways to do that for you:
FolderBrowserDialog1.ShowDialog()
Dim myPath As String = FolderBrowserDialog1.SelectedPath
' get all files for a folder
Dim files = Directory.GetFiles(myPath)
' get all files for all sub folders
Dim files = Directory.GetFiles(myPath, "*.*",
System.IO.SearchOption.AllDirectories)
' get certain file types for folder and subs
Dim files = Directory.GetFiles(myPath, "*.jpg",
System.IO.SearchOption.AllDirectories)
You also are not going to be able to simply assign the results to a ReadOnlyCollection like that, because they are ReadOnly. The collection needs to be created/instanced with the complete list:
Dim counter As new ReadOnlyCollection(Of String)(files)
I got a question in Visual Basic 2010: How can I copy all subfolders (only the subfolders, not the main folder) into another folder?
You need to recursively iterate through all the files and folders and copy them. This method should do the job for you:
Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
' If the destination folder don't exist then create it
If Not System.IO.Directory.Exists(destinationPath) Then
System.IO.Directory.CreateDirectory(destinationPath)
End If
Dim fileSystemInfo As System.IO.FileSystemInfo
For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
Dim destinationFileName As String =
System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
' Now check whether its a file or a folder and take action accordingly
If TypeOf fileSystemInfo Is System.IO.FileInfo Then
System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
Else
' Recursively call the mothod to copy all the neste folders
CopyDirectory(fileSystemInfo.FullName, destinationFileName)
End If
Next
End Sub
System.IO has two classes that you can use in a recursive fashion to do this all from code.
DirectoryInfo
FileInfo
DirectoryInfo has two methods that are relevant:
GetDirectories
GetFiles
FileInfo has a CopyTo method
Given those objects and methods and a bit of creative recursion you should be able to copy the stuff fairly easily.
This seems to be the simplest solution:
For Each oDir In (New DirectoryInfo("C:\Source Folder")).GetDirectories()
My.Computer.FileSystem.CopyDirectory(oDir.FullName, "D:\Destination Folder", overwrite:=True)
Next oDir
1 Line:
My.Computer.FileSystem.CopyDirectory(txtSourceDirectory.Text, txtDestinationDirectory.Text, True)
I am trying to search the C:\ drive for all files with a certain extension. I am using the following code which is working fine, however when it encounters an error the whole process stops rather than continuing with the scan. (running in backgroundworker, hence the invoke)
Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)
'Determine if the current folder contains any sub folders
Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)
For Each subFolder As String In subFolders
ScanFiles(subFolder, fileExtension)
Next
For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
Next
End Sub
How can I make this code continue once an error is encountered?
If you don't have access to explore C drive itself then you are out of luck. but if you are getting an exception because you don't have access to some child folder in the tree, you can avoid it by putting your code in an try-catch block.
Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)
'Determine if the current folder contains any sub folders '
try
Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)
For Each subFolder As String In subFolders
ScanFiles(subFolder, fileExtension)
Next
For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
Next
catch (Ex As UnauthorizedAccessException)
'Ignore Access Errors '
end try
End Sub