1

I am checking for the existence of a file in the XML backup of my iTunes library.

How do I change this string

E:\Entertainment\Music\Latin\100% Azucar The Best of Celia Cruz & La Sonora Matancera/08 Mi Soncito.mp3

to this

E:/Entertainment/Music/Latin/100%25%20Azucar%20%20The%20Best%20of%20Celia%20Cruz%20&%20La%20Sonora%20Matancera/08%20Mi%20Soncito.mp3

Changes:

  1. Space becomes %20
  2. Backslash becomes Forward-slash
  3. % (percentage sign) became %25
5
  • You forgot that & became &. Which is kinda weird - I would expect it to become %26. Commented Sep 10, 2010 at 21:12
  • @Vilx: It becomes %26 when you URIEncode it. Perhaps you're thinking of HTMLEncoding. Commented Sep 10, 2010 at 21:31
  • @Vilx: On second look, you're right. Perhaps the string needs to be HTMLEncoded, too. Commented Sep 10, 2010 at 21:32
  • but is E: a protocol? If it is an URI it should be like file:///E:/Entertainment/Music/Latin/100%25%20Azucar%20%20The%20Best%20of%20Celia%20Cruz%20&%20La%20Sonora%20Matancera/08%20Mi%20Soncito.mp3 Commented Sep 10, 2010 at 21:42
  • 1
    @Caspar: You are correct. In order to load it up into a Uri, we may need to prefix with the "file:" protocol. In this case, it looks like we'd want to remove it after we finish our processing, or else we'd load it up and extract the pieces individually. I'm thinking the latter, actually. Commented Sep 11, 2010 at 0:16

5 Answers 5

7

Looks like two separate things:

1) Replacing backslash with forward slash.

2) Applying URI-encoding.

The first is obvious. For the second, you have some choices, including Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode.

edit

3) As Vilx noticed, there seems to be HtmlEncoding of the filename portion.

Maybe the best answer is to use the Uri class to slice and dice the parts, encoding them as needed.

Sign up to request clarification or add additional context in comments.

Comments

4

I think I found the key part of his problem. It's kinda hidden there, being the first sentence and all, so nobody noticed it much. XD

I am checking for the existence of a file in the XML backup of my iTunes library.

It seems to me that we are dealing with some rogue iTunes-XML encoding, which has messed up a bit somewhere along the line. My advice would be to go the other way round - decode the iTunes string (first through HtmlDecode, then UrlDecode) and then compare it to the original string you are looking for. This should be able to cope with most mis-encodings of iTunes.

Comments

2

A very simple route:

filepath.Replace("\", "/").Replace("%", "%25").Replace(" ", "%20").Replace("&", "&")

2 Comments

This only works for this specific case. When other characters are encoded, this fails.
I couldn't find an enconding scheme that matched was iTunes was using, so I had to go this route1
0

%20 means space character. i see that you want to do something more with this string, so may i ask you what tools you'll prefer to use? regex? string replaces?

4 Comments

that's kind of what he's asking us ;)
Tomasz, URI-encoding does replace spaces with %20, but it also replaces many other characters. This means String.Replace is not a good solution. As for RegExp, that's overkill.
that's why i asked what tools he'd prefer, so that i can write proper code ;]
I think the idea here is that he wants it to work, so he's asking you to select the tool.
0

You do it with Uri.EscapeDataString() static method. Super easy.

1 Comment

No, this will escape the backslashes instead of converting them. In other words, you still need to do a Replace first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.