Combobox just file name (without extention or path) VB.Net - vb.net

I got the following code which is nice and all however it returns the entire path as well as name
For Each s As String In System.IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC\BASIS\")
combobox1.Items.Add(s)
Next
What I'm after is the file name only and preferably without its extention...
UPDATE
For Each s As String In GetFileNameWithoutExtension("C:\VTS\TREADSTONE LT\ATC\BASIS\")
combobox1.Items.Add(s)
Next

You'll need to use the Path class inside the loop:
Dim dir = "C:\VTS\TREADSTONE LT\ATC\BASIS\"
For Each file As String In System.IO.Directory.GetFiles(dir)
combobox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next

Change your code to:
For Each s As String In System.IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC\BASIS\")
s = s.Substring(0,s.LastIndexOf("."))
combobox1.Items.Add(s)
Next

Dim di As New IO.DirectoryInfo(My.Application.Info.DirectoryPath + "\softbelldata")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
If dra.Extension = ".mdb" Then
txtyear.Items.Add(System.IO.Path.GetFileNameWithoutExtension(dra.Name))
End If
Next

Related

Directory.GetFiles | Get only the specific file

I want to copy a specific file only but it copies all files in the directory. Is there any way to achieve it?
here is my code:
Dim dir As DirectoryInfo = New DirectoryInfo("\\SERVER-PC\BrokerDatabase\BrokerDatabase\Attachments")
For Each fi As FileInfo In dir.GetFiles()
fi.CopyTo("D:\" + fi.Name)
Next
It copies all files in the directory because you're looping the .CopyTo without any condition. If the filename of the file you are looking for is static, add a condition with it so it won't copy all the files.
Dim dir As DirectoryInfo = New DirectoryInfo("\\SERVER-PC\BrokerDatabase\BrokerDatabase\Attachments")
For Each fi As FileInfo In dir.GetFiles()
If fi.Name = "FileToBeCopied.txt" Then
fi.CopyTo("D:\" + fi.Name)
End If
Next
Another (fancy) way,
Dim allSeries As IEnumerable(Of String) =
Directory.EnumerateFiles(root, file, SearchOption.AllDirectories)
If allSeries.Count = 0 Then
MsgBox("not found.")
End If
where "root" is a String with the file path, and "file" is a String with the file name

display all excel file in folder

I want to review all the Excel files inside a folder to listbox, noting that the Excel includes more than one version.
I used the following code and it didn't work
Dim di As New IO.DirectoryInfo("D:\EXCEL")
Dim aryFi As IO.FileInfo() = di.GetFiles("Excel Worksheets|*.xls; *.xlsx; *.xlsm")
Dim fi As IO.FileInfo
For Each fi In aryFi
ListBox1.Items.Add((fi))
Next
Modified slightly from this example: https://stackoverflow.com/a/3527717/1920035
Private Function GetExcelFiles(ByVal directoryInfo As IO.DirectoryInfo) As IO.FileInfo()
If (directoryInfo Is Nothing) Then
Throw New ArgumentNullException(NameOf(directoryInfo))
End If
Dim files As IEnumerable(Of IO.FileInfo) = directoryInfo.GetFiles()
Dim excelFileExtensions() As String = {".xlsx", ".xlsm", ".xlsb", ".xltm", ".xlam", ".xls", ".xla", ".xlb", ".xlc", ".xld", ".xlk", ".xll", ".xlm", ".xlt", ".xlv", ".xlw"}
return files.Where(Function(file) excelFileExtensions.Contains(file.Extension))
End Function
What this does is:
Get all files from the directory info
Declare a collection of file extensions to check against
Return only the files where the extension exists in the file extensions collection
Important to note - It may be worthwhile moving the excelFileExtensions so that it is a private readonly variable at a higher scope. It isn't much, but if you're running this a lot then it will make a difference.

How to get most recent modified date pdf file and display on VB from?

i'm trying to get vb to read the most recent modified pdf file in a specific folder and display the pdf file on my vb form. I only able to create a simple pdf display on my vb form and i'm stuck at this. Can anyone help?
Unable to find solution to my problem.
Dim testFile As System.IO.FileInfo
Dim fileName As String
Dim folderPath As String
Dim fullPath As String
testFile = My.Computer.FileSystem.GetFileInfo("C:\Users\example.pdf")
folderPath = testFile.DirectoryName
fileName = testFile.Name
fullPath = My.Computer.FileSystem.CombinePath(folderPath, fileName)
AxAcroPDF1.src = fullPath
my vb form should display the PDF based on the most recent modified file.
You can use the IO.DirectoryInfo class to get every IO.FileInfo in a directory while specifically targeting PDF files, then use LINQ to order them by their LastWriteTime, and then get the last file from the collection:
Dim folder As IO.DirectoryInfo = New IO.DirectoryInfo("my folder path here")
Dim lastModifiedPdf As IO.FileInfo = folder.GetFiles("*.pdf").OrderBy(Function(f) f.LastWriteTime).LastOrDefault()
If lastModifiedPdf IsNot Nothing Then
'....
End If
You need to call two sets of functions to achieve this.
A. Directory.GetFiles - this will list all the files in a directory, and it has options to provide a search pattern and also look in sub-folders.
B. File.GetLastWriteTime - this will return the last modified time of the file you pass to it.
You can put these functions together like:
Private Function GetLatestModifiedFileName(searchFolder As String) As String
Dim retVal = "<empty>"
Dim filesInDirectory() = Directory.GetFiles(searchFolder)
Dim latestModifiedtime As DateTime = DateTime.MinValue
For Each fileInDirectory As String In filesInDirectory
Dim currentFileModifiedTime As DateTime = File.GetLastWriteTime(fileInDirectory)
If (currentFileModifiedTime > latestModifiedtime) Then
retVal = fileInDirectory
latestModifiedtime = currentFileModifiedTime
End If
Next
Debug.Print("File: '{0}' was last modified on: '{1}'", retVal, latestModifiedtime)
Return retVal
End Function
and ultimately call this function using:
Dim lastModifiedFileName = GetLatestModifiedFileName("D:\Documents\")
The variable lastModifiedFileName will contain the full path to the file that has the latest modified date/time.

looping inside folders and subfolders .net

Dim txtbx As String = "E:\_net_programs\test"
For Each d As String In Directory.GetDirectories(txtbx)
'For Each di As file
'Next
'MessageBox.Show(d)
'Dim folder As DirectoryInfo in directory
Next
Please help me loop inside this folder "E:_net_programs\test" and open every sub folder
There's the code :
Dim txtbx As String = "E:\_net_programs\test"
For Each File In Directory.GetFiles(txtbx, "*", SearchOption.AllDirectories)
MsgBox(File)
Next
If you don't want a loop of String but of FileInfo to be able to get their attributes (name, parent directory, extension, etc.), there's the code :
Dim txtbx As String = "E:\_net_programs\test"
For Each File As FileInfo In New DirectoryInfo(txtbx).GetFiles("*", SearchOption.AllDirectories)
MsgBox(File.FullName)
Next

how to check if ANY directory exists without knowing a specific directory name, but rather just the folder that the folder might be in

In vb.net how do you check if a ANY directory exists inside a directory
I would need to know if there is a folder inside the c:\windows directory (WITHOUT knowing if there is ANY directory is in there).
So you want to check to see if there are subdirectories in a directory? Fair enough:
Dim hasSubDirectories as Boolean = My.Computer.FileSystem.GetDirectories(parentDir).Count > 0
You can use the DirectoryInfo class inside the System.IO namespace.
Example:
Dim path As String = "C:\Windows"
Dim directoryInfo As New DirectoryInfo(path)
Dim dirInfos() As DirectoryInfo = directoryInfo.GetDirectories()
If (dirInfos.Length > 0) Then
' you have directories, do what you want
End If
'or iterate over directories
For Each dirInfo As DirectoryInfo In dirInfos
' do something with each directory
Next
Rather than use a VB-specific function like mattbasta suggests, it's just as easy to use the System.IO.Directory class, which is part of the BCL and would be familiar to any other .NET developer.
Dim hasSubDirectories = System.IO.Directory.GetDirectories(parentPath).Length > 0
Problem is that i cant convert to string
Dim path As String = "..\..\..\Tier1 downloads\CourseVB\"
If countNumberOfFolders > 0 Then 'if there is a folder then
' make a reference to a directory
Dim di As New IO.DirectoryInfo(path)
Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
Dim dra As IO.DirectoryInfo
'list the names of all files in the specified directory
For Each dra In diar1
Dim lessonDirectoryName() As Lesson
lessonDirectoryName(0).lessonName = dra
Next
'the the lesson is an object, and lessonName is the property of type string. How do i convert the directoryInfo to string?