Results 1 to 10 of 10

Thread: [RESOLVED] [VB 2008] Spliting a String??? Possible?

  1. #1

    Thread Starter
    Member bigal199's Avatar
    Join Date
    Jun 2009
    Location
    Waterford, WI
    Posts
    36

    Resolved [RESOLVED] [VB 2008] Spliting a String??? Possible?

    Hey guys... I'm back with anotherish question...

    Lets say I have this in a directory:

    music.mp3
    music 1.mp3
    music 2.mp3
    music 3.mp3

    Since it is sorted like this is there a way to read the first file and put it into a string. then split the string...

    "music.mp3"

    into "music" and ".mp3"

    This way I can add the " 1", " 2", " 3" to the string to delete those files.

    ===========

    So I want it to read only the first file.
    delete all files with the " 1", " 2", " 3", ect... added on
    then pause...
    read the next file in the directory .... And start over until all the files in the directory have been read...

    Thanks for all future help, (tg and tassa... )

    -Bigal

  2. #2
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [VB 2008] Spliting a String??? Possible?

    vb/net Code:
    1. Sub Main()
    2.         Dim filenames As String() = {"music.mp3", "music 1.mp3", "music 2.mp3", "music 3.mp3"}
    3.         For i As Integer = 0 To filenames.Count - 1
    4.             'I'm setting it to split the string every time it finds a dot "."
    5.             'c is just a redundency, it's just pointing out that is a Char.
    6.             Dim name As String() = filenames(i).Split(New Char() {"."c}, StringSplitOptions.RemoveEmptyEntries)
    7.             For c As Integer = 0 To name.Count - 1
    8.                 Console.Write(name(c) & " \ ")
    9.             Next
    10.             Console.WriteLine()
    11.         Next
    12.         Console.ReadKey()
    13.     End Sub
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  3. #3

    Thread Starter
    Member bigal199's Avatar
    Join Date
    Jun 2009
    Location
    Waterford, WI
    Posts
    36

    Re: [VB 2008] Spliting a String??? Possible?

    ok i just can't understand this code?!?!?

    This is what I need my program to do...

    Read first file in a directory... example: music.mp3 as string fullfilename
    split the string... example: music as string filename & .mp3 as string ext
    then search the directory for:

    (filename + " 1" + ext) example: music 1.mp3
    (filename + " 2" + ext)
    (filename + " 3" + ext)
    (filename + " 4" + ext)
    (filename + " 5" + ext)
    (filename + " 6" + ext)
    (filename + " 7" + ext)
    (filename + " 8" + ext)
    (filename + " 9" + ext)

    If any of those files exsist delete them...
    (at any point I need music.mp3 to get moved to C:\Moved\)
    then run the program again, and again... until no files are left in the directory...

    This will move the ones I want to keep and delete the ones I want to delete!

    (thanks for putting up with me tassa)
    ...and everyone else...

    -Bigal

  4. #4
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [VB 2008] Spliting a String??? Possible?

    I suggest you to use wild card - *

    If you say
    vb Code:
    1. strFile = Dir("*.mp3")
    strFile will have the first file having the extension .mp3. Then you can have a loop to check in rest of the directory.
    vb Code:
    1. Do While strFile <> ""
    2. strFile = Dir()
    Microsoft Techie

  5. #5
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [VB 2008] Spliting a String??? Possible?

    Hey Bigal. The String.Split method is quite easy to use... It splits a sentence into an array depending on which character you want it to split, for example.

    I.am.small.and.cute.

    If you used the split method to split this sentence into words every time it finds a dot "." then the code line would go like this:

    vb.net Code:
    1. Dim words As String() = ("I.am.small.and.cute").String.Split(New Char {"."c})

    So we would have each word in an array position. For instance, if you wanted to get the words you would just have to loop around the array getting the words... Something like this:

    vb.net Code:
    1. For i As Integer = 0 To words.Count - 1
    2. Using sw As New IO.StreamWriter("C:\Words.txt")
    3. sw.Write(words(i) & " ")
    4. End Using
    5. Next

    That code writes all the array words into one sentence. You should try reading the msdn documentation on String.Split method .
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [VB 2008] Spliting a String??? Possible?

    Why use String.Split, when IO.Path.GetFileNameWithoutExtension and IO.Path.GetExtension are available split filenames.

    Code:
            For Each f As String In IO.Directory.GetFiles("C:\Temp\")
                Dim filename As String = IO.Path.GetFileNameWithoutExtension(f)
                Dim ext As String = IO.Path.GetExtension(f)
    
                MessageBox.Show(filename + ext)
    
            Next

  7. #7
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [VB 2008] Spliting a String??? Possible?

    Oh, I hadn't thought about it. That's a much better way. I had forgotten about that one
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  8. #8

    Thread Starter
    Member bigal199's Avatar
    Join Date
    Jun 2009
    Location
    Waterford, WI
    Posts
    36

    Re: [VB 2008] Spliting a String??? Possible?

    WOW!!!! I got it!!!! This was way easier then we were making it!!!

    Code:
    Imports System
    Imports System.IO
    
    
    Public Class Form1
    
        Private Sub Startbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For Each f As String In IO.Directory.GetFiles(textbox1.text)
                Dim filename As String = IO.Path.GetFileNameWithoutExtension(f)
                Dim ext As String = IO.Path.GetExtension(f)
    
                File.Delete(TextBox1.Text + "\" + filename + " 1" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 2" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 3" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 4" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 5" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 6" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 7" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 8" + ext)
                File.Delete(TextBox1.Text + "\" + filename + " 9" + ext)
    
            Next
    
        End Sub
    
        Private Sub Browsebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            FolderBrowserDialog1.ShowDialog()
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End Sub
    End Class
    Now... If I could have it do same as above but to all subdirectories as well?
    (somehow change textbox1.text to the path of a subdirectory and have it run again??)

    If not I'm happy!!!!

    Thanks for all of the pain of helping me!!!

    -Bigal

    (i'll mark as resolved in 12hrs if no one gets back!)
    Last edited by bigal199; Jul 3rd, 2009 at 10:08 AM. Reason: info:

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [VB 2008] Spliting a String??? Possible?

    Change your loop a little bit to do subdirectories:

    Code:
    For Each f As String In IO.Directory.GetFiles("C:\Temp\","*.mp3",IO.SearchOption.AllDirectories )

  10. #10

    Thread Starter
    Member bigal199's Avatar
    Join Date
    Jun 2009
    Location
    Waterford, WI
    Posts
    36

    Re: [VB 2008] Spliting a String??? Possible?

    got it... Thanks everyone

    Code:
    For Each f As String In Directory.GetFiles(TextBox1.Text, "*.*", IO.SearchOption.AllDirectories)
                    Dim dirname As String = Path.GetDirectoryName(f)
                    Dim filename As String = IO.Path.GetFileNameWithoutExtension(f)
                    Dim ext As String = IO.Path.GetExtension(f)
    
    
                    File.Delete(dirname + "\" + filename + " 1" + ext)
                    File.Delete(dirname + "\" + filename + " 2" + ext)
                    File.Delete(dirname + "\" + filename + " 3" + ext)
                    File.Delete(dirname + "\" + filename + " 4" + ext)
                    File.Delete(dirname + "\" + filename + " 5" + ext)
                    File.Delete(dirname + "\" + filename + " 6" + ext)
                    File.Delete(dirname + "\" + filename + " 7" + ext)
                    File.Delete(dirname + "\" + filename + " 8" + ext)
                    File.Delete(dirname + "\" + filename + " 9" + ext)
    
    
                Next
    -Bigal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width