Tiny problem with file extensions

jango_fett

Member
Joined
Jun 15, 2005
Messages
23
Programming Experience
Beginner
Tiny problem with file extensions - [RESOLVED]

Hi, there. I have created a function that returns a list of files within a specified directory. The IO.FileInfo.Name property is adequate because through it I can obtain only the names of the files without its whole path from root. However, it still gives away the files' extension. Is there a way to show only the names without the extensions? Here's the function I' created. Many thanks.

HTML:
Private Function ListAllFileNames(ByVal MyDir As String) As ArrayList
   Dim MyList As New ArrayList, MyInfo As IO.FileInfo
   If IO.Directory.Exists(MyDir) Then
	  Dim MyFiles() As String = IO.Directory.GetFiles(MyDir)
	  For Each MyFile As String In MyFiles
		 MyInfo = New IO.FileInfo(MyFile)
		 MyList.Add(MyInfo.Name)
	  Next
   End If
   Return MyList
End Function
 
Last edited:
as you read in each file name take the returned string and read each character to a new string

be sure to have it stop as soon as it hits the dot

have a look at the Mid() function
 
How about this?

VB.NET:
 Dim items() AsString 
 
items = Directory.GetFiles("c:\Windows")
 
Dim file AsString
 
ForEach file In items
 
Dim w() AsString = file.Split(".")
 
ListBox1.Items.Add(w(0))
 
Next


I also made an example so you can take a look in deeds the above code.

Cheers ;)
 

Attachments

  • ListTheFiles.zip
    21.7 KB · Views: 27
just small help

I have a function in my app that you may need in your app.
Just copy the function in your app code and set the parameters corecly; for yuor case

Label4.Text = TrimText("the file name.xtn", "", "", "", ".", -1, -1)
or
Label4.Text = TrimText("the file name.xtn", ".", "", "", "", -1, -1)

ether way is fine.

Private Function TrimText(ByVal theString As String, ByVal trimFromChar As String, ByVal trimToChar As String, ByVal trimUptoLastChar As String, ByVal trimFromLastChar As String, ByVal fromBeginning As Integer, ByVal fromEnd As Integer) As String

Dim count As Integer

If theString <> "" Then

If trimFromChar <> "" And theString.IndexOf(trimFromChar) <> -1 Then

Return theString.Substring(0, theString.IndexOf(trimFromChar))

ElseIf trimToChar <> "" And theString.IndexOf(trimToChar) <> -1 Then

Return theString.Substring(theString.IndexOf(trimToChar) + 1)

ElseIf trimUptoLastChar <> "" And theString.IndexOf(trimUptoLastChar) <> -1 Then

For count = 0 To theString.Length - 1

If trimUptoLastChar = theString.Substring(theString.Length - 1 - count, 1) Then

Return theString.Substring(theString.Length - count)

End If

Next

ElseIf trimFromLastChar <> "" And theString.IndexOf(trimFromLastChar) <> -1 Then

For count = 0 To theString.Length - 1

If trimFromLastChar = theString.Substring(theString.Length - 1 - count, 1) Then

Return theString.Substring(0, theString.Length - 1 - count)

End If

Next

ElseIf fromBeginning > -1 Then

Return theString.Substring(0, fromBeginning)

ElseIf fromEnd > -1 Then

Return theString.Substring(0, theString.Length - fromEnd)

Else

Return ""

End If

Else

Return ""

End If

End Function

 
The use of GetFileNameWithoutExtension seems fairly self-evident.
VB.NET:
For Each filePath As String In IO.Directory.GetFiles(myFullyQualifiedFolderName)
	myList.Add(IO.Path.GetFileNameWithoutExtension(filePath))
Next
 
Thanks a lot, guys!

Thanks for all your help. So many ways to get the job done. I tried all of them and they all seem to work. I wasn't aware of the GetFileNameWithoutExtension method. I guess I need to pay more attention to those namespaces. Thanks, Kulrom for the zip file. And all of you for your assistance. I'll try to make more intelligent questions next time. ;)
Best to you!

DK
 
jmcilhinney said:
The use of GetFileNameWithoutExtension seems fairly self-evident.
VB.NET:
For Each filePath As String In IO.Directory.GetFiles(myFullyQualifiedFolderName)
	myList.Add(IO.Path.GetFileNameWithoutExtension(filePath))
Next
I didn't mean to belittle. I didn't know of the existence of that function either until I read Neal's post. I just meant that once you know of it, the name Path.GetFileNameWithoutExtension pretty much tells you what it does.
 
Yes, I guess the GetFileNameWithoutExtension pretty much tells what it does, and it's the shortest and fastest way to get the job done. Of course you didn't mean to belittle. I never took it that way. It's just that the question I posted seems so dumb especially after such an easy solution emerges. Thanks once more.

DK
 
Back
Top