You may want to have a look at this thread on a similar problem: Help with smart truncating of long filenames