Fastest subdirectory listing - vb.net

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

Related

Restoring File From Temp Folder To Original Folder And File Name

I move a file to the temp folder (Path.GetTempPath). Later, the user wishes to restore the file to its original location using an application option. I can get the file name without any trouble. How do I get the original file path? In the file properties there is a Path item but it is always the current path. Since I am already setting an EXIF tag, I thought of using some other EXIF tag with the original path and file name. EXIF seems counter productive and there must be another way. I have looked at similar questions but none seem to solve this issue.
Why not just put another file in the temp folder with the same name, different extension and inside it put the original location?
Dim toMove = "c:\myfolder\some.txt"
Dim temp = Path.GetTempFileName() 'string of eg c:\user\appdata\temp\file.tmp..
Dim loc = Path.ChangeExtension(temp, "loc") 'string of eg c:\user\appdata\temp\file.loc..
File.Move(toMove, temp) 'move c:\myfolder\some.txt -> c:\user\appdata\temp\file.tmp
File.WriteAllText(loc, toMove) 'write "c:\myfolder\some.txt" string into file at c:\user\appdata\temp\file.loc
And then to move it back
Dim toMoveBack = "..some tmp file path.." 'e.g. c:\user\appdata\temp\file.tmp
Dim loc = Path.ChangeExtension(toMoveBack, "loc") 'e.g. c:\user\appdata\temp\file.loc
Dim origLoc = File.ReadAllText(loc) 'origLoc is now a value of c:\myfolder\some.txt
File.Move(toMoveBack, origLoc) 'move c:\user\appdata\temp\file.tmp -> c:\myfolder\some.txt
File.Delete(loc) 'remove c:\user\appdata\temp\file.loc

unable to sort the files getting from system in visual basic

I am trying to get files from system in my application. But when retrieving files it used to come but not in sorted order. I want all my files to come in sorted order. I am very new to this visual basic and i have tried so many things but unable to get files in sorted order. Please Help me out, thanks in advance.
Here is my code : -
Dim path As String = "C:/UTRelOFA/Reva Test/src/00000178"
Dim oFolder As New DirectoryInfo(path)
Dim arr = oFolder.GetFiles(".tif")
Array.Sort(arr)
Cosole.writeline(arr)
I have used so many things and refered this link :
Sorting Directory.GetFiles()
The question here is whether you should be calling DirectoryInfo.GetFiles or Directory.GetFiles. If you call the former then you get a FileInfo array, so if you want to sort by file name then you have to specify that explicitly, e.g.
Dim folderPath = "C:\UTRelOFA\Reva Test\src\00000178"
Dim folder As New DirectoryInfo(folderPath)
Dim files = folder.GetFiles("*.tif")
Array.Sort(files, Function(fi1, fi2) fi1.Name.CompareTo(fi2.Name))
If you don't actually need FileInfo objects but rather just the file paths, don;t use a DirectoryInfo, e.g.
Dim folderPath = "C:\UTRelOFA\Reva Test\src\00000178"
Dim filePaths = Directory.GetFiles(folderPath, "*.tif")
Array.Sort(filePaths)

Read File name and put it into Variable

I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If

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

Returning subfolder names within dated folder

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)