Need Help With Parsing Strings
-
Hi, everyone. I'm working on a program to backup playlists in iTunes. The main criteria for the program is that it maintains the same directory structure as the iTunes library. For example, if the song's full filename is E:\iTunes\Queen\The Works\01 Radio GaGa.mp3 and the destination directory is C:\Temp, then the destination filename should be C:\Temp\Queen\The Works\01 Radio GaGa.mp3. The problem is, I know the method I'm using to build the filenames is pretty inefficient. Here's the relavent code I'm using to build the filenames:
foreach(IITFileOrCDTrack iTrack in iTracks)
{
// Find the filename
string srcPath = iTrack.Location;// Parse the full filename and remove anything
// before the artist folder.
// eg. "C:\iTunes\Queen\The Works\01 Radio GaGa.mp3" becomes
// "Queen\The Works\01 Radio GaGa.mp3"
int a = srcPath.LastIndexOf('\\');
int b = srcPath.LastIndexOf('\\', a - 1);
int start = srcPath.LastIndexOf('\\', b - 1);// Strip the beginning path from the filename.
string destPath = srcPath.Remove(0, start);// Insert the destination folder.
destPath = destPath.Insert(0, tmpPath);// Remove the filename and check if the destination folder exists.
start = destPath.LastIndexOf('\\');
string tmpDest = destPath.Substring(0, start);
string tmpSongName = destPath.Substring(start + 1);There has got to be a better way to parse the strings than using this method. I've read about the StringBuilder class and it should work, however I'm not certain how I'd set that up in this situation. Does anyone have any ideas on how I could do this? Thanks in advance. :) Flynn
-
Hi, everyone. I'm working on a program to backup playlists in iTunes. The main criteria for the program is that it maintains the same directory structure as the iTunes library. For example, if the song's full filename is E:\iTunes\Queen\The Works\01 Radio GaGa.mp3 and the destination directory is C:\Temp, then the destination filename should be C:\Temp\Queen\The Works\01 Radio GaGa.mp3. The problem is, I know the method I'm using to build the filenames is pretty inefficient. Here's the relavent code I'm using to build the filenames:
foreach(IITFileOrCDTrack iTrack in iTracks)
{
// Find the filename
string srcPath = iTrack.Location;// Parse the full filename and remove anything
// before the artist folder.
// eg. "C:\iTunes\Queen\The Works\01 Radio GaGa.mp3" becomes
// "Queen\The Works\01 Radio GaGa.mp3"
int a = srcPath.LastIndexOf('\\');
int b = srcPath.LastIndexOf('\\', a - 1);
int start = srcPath.LastIndexOf('\\', b - 1);// Strip the beginning path from the filename.
string destPath = srcPath.Remove(0, start);// Insert the destination folder.
destPath = destPath.Insert(0, tmpPath);// Remove the filename and check if the destination folder exists.
start = destPath.LastIndexOf('\\');
string tmpDest = destPath.Substring(0, start);
string tmpSongName = destPath.Substring(start + 1);There has got to be a better way to parse the strings than using this method. I've read about the StringBuilder class and it should work, however I'm not certain how I'd set that up in this situation. Does anyone have any ideas on how I could do this? Thanks in advance. :) Flynn
StringBuilder builds strings, it doesn't parse them. System.IO.Path contains static methods to get the filename or directory name only from a path. the Split method on a string will turn it into an array of directories if you split on \.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
StringBuilder builds strings, it doesn't parse them. System.IO.Path contains static methods to get the filename or directory name only from a path. the Split method on a string will turn it into an array of directories if you split on \.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Erm, yeah. Parsing wasn't quite the word I was looking for, just the best fit at the time, heh. I'll take a look at the Split method. I'm hoping once I get this part fixed and the code cleaned up a little that I can write an article on it. Mostly from the angle of working with the iTunes COM object, and a useful utility for the community. :) Thanks again. :-D Flynn
-
Hi, everyone. I'm working on a program to backup playlists in iTunes. The main criteria for the program is that it maintains the same directory structure as the iTunes library. For example, if the song's full filename is E:\iTunes\Queen\The Works\01 Radio GaGa.mp3 and the destination directory is C:\Temp, then the destination filename should be C:\Temp\Queen\The Works\01 Radio GaGa.mp3. The problem is, I know the method I'm using to build the filenames is pretty inefficient. Here's the relavent code I'm using to build the filenames:
foreach(IITFileOrCDTrack iTrack in iTracks)
{
// Find the filename
string srcPath = iTrack.Location;// Parse the full filename and remove anything
// before the artist folder.
// eg. "C:\iTunes\Queen\The Works\01 Radio GaGa.mp3" becomes
// "Queen\The Works\01 Radio GaGa.mp3"
int a = srcPath.LastIndexOf('\\');
int b = srcPath.LastIndexOf('\\', a - 1);
int start = srcPath.LastIndexOf('\\', b - 1);// Strip the beginning path from the filename.
string destPath = srcPath.Remove(0, start);// Insert the destination folder.
destPath = destPath.Insert(0, tmpPath);// Remove the filename and check if the destination folder exists.
start = destPath.LastIndexOf('\\');
string tmpDest = destPath.Substring(0, start);
string tmpSongName = destPath.Substring(start + 1);There has got to be a better way to parse the strings than using this method. I've read about the StringBuilder class and it should work, however I'm not certain how I'd set that up in this situation. Does anyone have any ideas on how I could do this? Thanks in advance. :) Flynn
There are many ways to do this. Here's a Regex approach: Match m = Regex.Match(srcPath, @".*\\(?.+)\\(?.+)\\(?.+)"); if (m.Success) { destFolder = tmpPath + m.Groups["Artist"].Value + @"\" + m.Groups["Album"].Value + @"\"; destPath = destPath + m.Groups["Song"].Value; } - Bitwise -- modified at 10:43 Wednesday 16th August, 2006