Problems with pushing tags to filenames

I've searched around and can't find an answer to my issue, so I figured I'd bug you guys. I've only learned how to use this program with just the basics, like pushing the artist to albumartist and some basic IF statements. Right now I have the issue that I'm trying to have one ridiculous format string to rename all my songs via tags. Usually this isn't an issue, but I've run into a problem.

This is my format string:
E:\Media\Music$if(%albumartist%,%albumartist%,%artist%)$if(%album%,(%year%) %album%,Misc Tracks)$if(%album%,$if(%discnumber%,$num(%discnumber%,1)-,)%track% - ,)%artist% - $if(%title%,%title%,'[untitled]')

As you can see, a lot of IFs. If there's an album artist, use it; otherwise use the artist. If there's an album, use it or put Misc Tracks (if you have a better idea for this folder name, let me know), if there's a disc number, append it to the track number, and if there's no title, call it untitled.

This doesn't seem to work on any tracks that have a slash in the album name, however. Everything else seems mostly fine. In the case of tracks with a backslash in the album name (in this case, Indie/Rock), it freaks out. This example, for instance, should have stayed exactly the same as it was already how I wanted it:

"E:\Media\Music\Various Artists(2013) IndieRock Playlist April (2013)\001 - Tessa Rose Jackson - (All The) King's Horses.mp3" ->
"E:\Media\Music\Various Artists(2013) IndieRock Playlist April (2013)\E\Media\Music\Various Artists(2013) IndieRock Playlist April (2013)\001 - Tessa Rose Jackson - (All The) King's Horses.mp3"

As you can see, it appends "E\Media\Music" to its current directory rather than what was intended.

So what exactly can I do about this? I'd like one end-all-be-all format string for my MP3s, and I felt like this was it. I'd appreciate any help or pointers.

If you know that it is always the files with a backslash in the album name then you could use the
function around all the expressions dealing with an album.
So ...$if(%album%,(%year%) %album%,Misc Tracks)
would become
$if(%album%,(%year%) $validate(%album%,_),Misc Tracks)

I doubt that the shown result string comes from the format string, ... because there is a text part doubled, which cannot be the result from your format string, so it will be a different error.

However ... there are forbidden characters for the filename, read there ...
Character remapping during conversion?

You can use one of the functions $replace or $regexp or $validate to replace forbidden characters with allowed character/s or remove them wholly.

Try out this format string ...

$validate('E:\Media\Music\'$if2(%ALBUMARTIST%,%ARTIST%)'\'$if(%ALBUM%,'('%YEAR%') '%ALBUM%,'Misc Tracks')'\'$if(%ALBUM%,[$num(%DISCNUMBER%,2)'-']%TRACK%' - ',)%ARTIST%' - '$if2(%TITLE%,'[untitled]'),'~')


Outstanding! Thanks guys. This did the trick. I wasn't aware of validate or replace, but I'll sure be using them in the future. Regexp is still a bit over my head, so I think I'll steer clear.