Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need Help With Parsing Strings

Need Help With Parsing Strings

Scheduled Pinned Locked Moved C#
helpjsontutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flynn Arrowstarr Regular Schmoe
    wrote on last edited by
    #1

    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

    C B 2 Replies Last reply
    0
    • F Flynn Arrowstarr Regular Schmoe

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • C Christian Graus

        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

        F Offline
        F Offline
        Flynn Arrowstarr Regular Schmoe
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • F Flynn Arrowstarr Regular Schmoe

          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

          B Offline
          B Offline
          Bitwise Gamgee
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups