|
-
Jul 2nd, 2009, 10:03 PM
#1
Thread Starter
Member
[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
-
Jul 2nd, 2009, 11:14 PM
#2
Fanatic Member
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
-
Jul 3rd, 2009, 01:05 AM
#3
Thread Starter
Member
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
-
Jul 3rd, 2009, 03:47 AM
#4
Fanatic Member
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()
-
Jul 3rd, 2009, 06:06 AM
#5
Fanatic Member
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 .
-
Jul 3rd, 2009, 07:19 AM
#6
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
-
Jul 3rd, 2009, 07:33 AM
#7
Fanatic Member
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
-
Jul 3rd, 2009, 10:07 AM
#8
Thread Starter
Member
Re: [VB 2008] Spliting a String??? Possible?
Last edited by bigal199; Jul 3rd, 2009 at 10:08 AM.
Reason: info:
-
Jul 3rd, 2009, 11:53 AM
#9
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 )
-
Jul 3rd, 2009, 12:27 PM
#10
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|