[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... :D)
-Bigal
Re: [VB 2008] Spliting a String??? Possible?
vb/net Code:
Sub Main()
Dim filenames As String() = {"music.mp3", "music 1.mp3", "music 2.mp3", "music 3.mp3"}
For i As Integer = 0 To filenames.Count - 1
'I'm setting it to split the string every time it finds a dot "."
'c is just a redundency, it's just pointing out that is a Char.
Dim name As String() = filenames(i).Split(New Char() {"."c}, StringSplitOptions.RemoveEmptyEntries)
For c As Integer = 0 To name.Count - 1
Console.Write(name(c) & " \ ")
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
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
Re: [VB 2008] Spliting a String??? Possible?
I suggest you to use wild card - *
If you say
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:
Do While strFile <> ""
strFile = Dir()
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:
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:
For i As Integer = 0 To words.Count - 1
Using sw As New IO.StreamWriter("C:\Words.txt")
sw.Write(words(i) & " ")
End Using
Next
That code writes all the array words into one sentence. You should try reading the msdn documentation on String.Split method ;).
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
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 ;)
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!) :D
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 )
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