Returning subfolder names within dated folder - vb.net

I'm trying to pull back a list of directories that sit inside a dated folder structure. Within each dated folder could be a number of 'Jobs' however i only want to return the name of the 1st level of folders
The below code gets to the right level of folder detail however the result displays the full path
For Each Dir As String In System.IO.Directory.GetDirectories("c:\Working")
Dim dirInfo As New System.IO.DirectoryInfo(Dir)
For Each sDir As String In System.IO.Directory.GetDirectories(dirInfo.ToString)
Dim sdirInfo As New System.IO.DirectoryInfo(sDir)
chkImpExp.Items.Add(sDir)
Next
Next
This would display the following
However i would just like to display the directory name to the right of the 3rd backslash (Westdale - 28023 - Cash+Spirit for example)
Hopefully this is enough information.
Many thanks

' renamed Dir to d as Dir() is already a function in Microsoft.VisualBasic
For Each d In System.IO.Directory.GetDirectories("c:\Working")
For Each sDir In System.IO.Directory.GetDirectories(d)
Dim di = New DirectoryInfo(sDir)
chkImpExp.Items.Add(di.Name())
Next
Next

Try:
chkImpExp.Items.Add(sdirInfo.Name)

Related

Search multiple files though sub folders VB.net

I'm having a hard time trying to figure it out how to find multiple files through sub folders, I'm looking for different file names and I think that's why I can't solve it.
"file_name" in the code is the name of the varible, I pull the name of the document from the data base which it's stored in a field without the extension and then when it finds the .docx/doc file it should convert it to pdf.
Dim di As DirectoryInfo = New DirectoryInfo("\\192.168.1.70\sisint\court\
agreements")
For Each fi In di.GetFiles(file_name, SearchOption.AllDirectories)
'seleccionamos los archivos con las extensiones de Word
If fi.Extension.ToUpper = ".DOC" Or fi.Extension.ToUpper = ".DOCX" Then
ListaArchivos.Add(fi.FullName)
ListaNombres.Add((fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)))
End If
Next
To search a directory with multiple sub directories for a file you can use the following line of code:
Dim folders As List(Of String) = New DirectoryInfo("C:\Test").EnumerateFiles("test.txt", SearchOption.AllDirectories).[Select](Function(d) d.FullName).ToList()
Replace "C:\Test" with the directory to search and "test.txt" with the filename to search for. If the same file name is in multiple directories all iles matching that name will be returned.
To find multiple file names, just loop through your list(file_name) replacing "test.txt" with the file. How you save all the output is up to you. You can add everything found to 1 large list and then convert them all to pdf's, or you can convert them as you find them.
Here is an example:
Private Function FindFiles() As List(Of String)
Dim allFilesFound As New List(Of String)
Dim dirSearch = "C:\Test"
Dim file_name As New List(Of String) From {"test.txt", "test2.txt"}
For Each file In file_name
allFilesFound.AddRange(New DirectoryInfo(dirSearch).EnumerateFiles(file, SearchOption.AllDirectories).[Select](Function(d) d.FullName).ToList())
Next
Return allFilesFound
End Function
Keep in mind that the more files in file_name and the farther up the Directory tree you start searching from, the longer this will take. If you are dealing with large amounts of files, then consider using recursion so you can find all files in one pass. You can take a look here Recursively search folder with subdirectories in .NET and modify the code demonstrated there to fit your search pattern.

Zip multiple files to one folder

So I am having trouble trying to reconcile this concept as every change I do doesn't seem to fix the issue.
I have multiline textbox and can enter multiple values separated by commas and here are the details:
Each value represents a folder
And each folder has multiple documents/other folders inside
All of these values are in one main directory (lets call it folder path)
For example say I enter in my textbox "65635,65636" each of those represent a folder in the directory i.e. "\folderpath\65635" and "\folderpath\65636", I am trying to zip these whole folders via DotNetZiplib, I know how to do this if I specifically reference one folder but is there anywhere to loop through the textbox to get the names of the folders and have the files added to one zipped folder?
Using zip As New ZipFile = new ZipFile
Dim files() As String = Directory.GetFiles(folderpath & textboxvalue)
For each textboxvalue in directory.getfiles
zip.Addfile(textboxvalue)
The zipfile function I have would know to loop through these as opposed to assuming it's one big file.
You must split textbox values in array first to get numbers separated by comma. Next you will need Combine your folder with this splited text values, creating path correctly.
Check if folder exists in system if Yes then get all files from Directory and for each filename zip it.
Something like this:
Using zip As New ZipFile("your zip filename")
For Each str As String In textboxvalue.Split(",")
Dim path as String = System.IO.Path.Combine(folderpath, str)
If System.IO.Directory.Exists(path) = False Then
Continue For
End If
Dim files() As String = Directory.GetFiles(path)
For Each fileName As String In files
zip.Addfile(fileName)
Next
Next
End Using

Remove Items in a listbox, data taken from the check box

I'm doing some experiments lately but I can't make this one work. Here's my code:
If cbHistory.Checked = True Then
Dim di As New DirectoryInfo("C:\Users\" & userName & "\AppData\Local\Google\Chrome\UserData")
Dim diar1 As FileInfo() = di.GetFiles("*.*", SearchOption.AllDirectories)
Dim dra As FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
listToDelete.Items.Add(dra)
Next
End If
My code takes a the list of files located in the specified location and put it in a list box. I have 2 more block of code like that (cbCache and cbCookies).
Now my problem is, I want that if I unchecked one of them, the cbHistory checkbox for example, it will remove the items it took from the directory. Now, I can't use listbox.items.clear() because it will remove everything in the list box. So what can I do here?
Any help would be appreciated. Thank you! :)

Get folder name in specified directory?

There is a one folder located in a directory, lets say its in
C:\Users\User\AppData\Roaming\Mozilla\Firefox\Profiles\
it's different on everyuser, for example 1rituum9.default named folder.
Tried this without any luck, it messages empty.
Dim filepath As String = "%Appdata%\Mozilla\Firefox\Profiles\"
Dim fi As New IO.FileInfo(filepath)
MessageBox.Show(fi.Name)
What is the correct way to get the folder name in specified directory ?
You can try this:
Dim filepath As String = Environment.GetEnvironmentVariable("appdata") & "\Mozilla\Firefox\Profiles\"
Dim di As New IO.DirectoryInfo(filepath)
MessageBox.Show(di.GetDirectories()(0).Name)
if there are more than one dirs u might need something like:
For Each Dir As IO.DirectoryInfo In di.GetDirectories()
ListBox1.Items.Add(Dir)
Next
Edit: Fixed Code Line 1 - see comments

Fastest subdirectory listing

I have an application where i want to get all subdirectories (first level) from a given path.
I use this code:
Dim di As New IO.DirectoryInfo(Application.StartupPath & "\Folderlist")
For Each d In di.EnumerateDirectories()
Console.WriteLine(d.ToString)
Next
As you can see, i just need the name of the folder, not some object with all info.
Is there any way i can speed this up?
Dim dirs() = Directory.GetDirectories(Application.StartupPath & "\Folderlist")
For Each dir In dirs
Dim parent = Path.GetFileName(dir)
Console.WriteLine(parent)
Next
MSDN -
Directory.GetDirectories
Gets the names of subdirectories (including their paths) in the
specified directory.
Path.GetFileName