Removing characters from a string
Removing characters from a string
Removing characters from a string
Removing characters from a string
Removing characters from a string
Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string
Removing characters from a string Removing characters from a string
Removing characters from a string
Go Back  Xtreme Visual Basic Talk > > > Removing characters from a string


Reply
 
Thread Tools Display Modes
  #1  
Old 01-14-2005, 10:56 PM
JJRJRM143 JJRJRM143 is offline
Freshman
 
Join Date: Jul 2004
Location: Fairhaven, Ma.
Posts: 42
Default Removing characters from a string


Here's the thing. I have a directory with 601 subdirectories in it all of which represent cd's of mine which I ripped. What I did was created a small program in which I basically point to a directory and it then loads all the subdirectories into a listbox. Here's my dilemna, it loads the entire path into the listbox. I only want the folder name, not the entire path. So I'm trying to use the remove function and am having a hard time as it doesn't seem to be doing much of anything. Here's my code

Code:
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click Dim strPath As String Dim dirFolder As System.IO.Directory Dim i As Integer Dim x As Integer Dim strHolder As String lstDirectories.Items.Clear() lblCount.Text = "0" fbdOpen.ShowDialog() strPath = fbdOpen.SelectedPath i = strPath.Length If strPath <> "" Then lstDirectories.Items.AddRange(dirFolder.GetDirectories(strPath)) For x = 0 To lstDirectories.Items.Count - 1 strHolder = Convert.ToString(lstDirectories.Items(x)) lstDirectories.Items.RemoveAt(x) strHolder.Remove(0, i + 1) lstDirectories.Items.Insert(x, strHolder) Next End If lblCount.Text = Convert.ToString(lstDirectories.Items.Count) End Sub

Also what is the difference between the Directory class and the DirectoryInfo class??? And also is there a simpler way of doing what I'm doing?? I mean instead of adding them and then looping through and shortening the path and then replacing them, is there a way to do this as they're being added??

Thanks in advance
Reply With Quote
  #2  
Old 01-15-2005, 08:32 AM
MKoslof's Avatar
MKoslofRemoving characters from a string MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Say you have one parent directory with multiple sub directories with files in it. You can use a recursive function like so, which uses the power of the overloaded methods. This example simply prints out the dir and file names..they can be added to a control, etc.:

Code:
Private Overloads Function Recursive(ByVal strPath As String) Dim oDir As New System.IO.DirectoryInfo(strPath) Dim oSubDir() As System.IO.DirectoryInfo Dim oFiles() As System.IO.FileInfo Dim i As Int32 oFiles = oDir.GetFiles For i = 0 To oFiles.Length - 1 Debug.WriteLine(oFiles(i).Name.ToString) Next oSubDir = oDir.GetDirectories() For i = 0 To oSubDir.Length - 1 Debug.WriteLine(oSubDir(i).Name.ToString) Call Recursive(oSubDir(i), 1) Next End Function Private Overloads Function Recursive(ByVal oDir As System.IO.DirectoryInfo, _ ByVal intLevel As Int32) Dim oSubDir() As System.IO.DirectoryInfo Dim oFiles() As System.IO.FileInfo Dim i As Int32 oFiles = oDir.GetFiles For i = 0 To oFiles.Length - 1 Debug.WriteLine(New String(" ", intLevel) & ">" & oFiles(i).Name.ToString) Next oSubDir = oDir.GetDirectories() For i = 0 To oSubDir.Length - 1 Debug.WriteLine(New String(" ", intLevel) & oSubDir(i).Name.ToString) Call Recursive(oSubDir(i), intLevel + 1) Next End Function Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Call Recursive("C:\Testing") End Sub
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Removing characters from a string
Removing characters from a string
Removing characters from a string Removing characters from a string
Removing characters from a string
Removing characters from a string
Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string Removing characters from a string
Removing characters from a string
Removing characters from a string
 
Removing characters from a string
Removing characters from a string
 
-->