4

How to convert file name with path to short file name (DOS style) in Adobe AIR?

For example convert next path

"C:\Program Files\Common Files\Adobe AIR\Versions\1.0\Resources\Adobe AIR Updater.exe"

to

"C:\PROGRA~1\COMMON~1\ADOBEA~1\VERSIONS\1.0\RESOUR~1\ADOBEA~1.EXE"

Is there any algorithm?

Maksym
  • 353
  • 1
  • 5
  • 14

2 Answers2

2

Assuming your text portion is a string variable, you can split it by using "\" as delimiter. Then, you will have an array which you can use to check if each block is longer than 8 characters. While looping the array you can chop the last characters of each long block and put ~1. Since you're in the loop, you can progressively add to a temporary variable all these changes which will give you the final edited result at the end.

The only part that's a bit tricky is to pay attention to .exe part at the end.

So, if I were you, I'd start reading on String.split(), String.substring(), for loop, arrays

Kumsal Obuz
  • 825
  • 5
  • 14
  • There's some more tricks to do it right. Have you seen ~2 in such paths? – alxx Aug 18 '11 at 17:43
  • Read this, http://en.wikipedia.org/wiki/8.3_filename especially the part they talk about ~ sign. I think it's perfectly safe to remove extra characters and add ~1. – Kumsal Obuz Aug 18 '11 at 18:21
  • Adding 1 is not safe in this case due to the _"undocumented hash"_: "Beginning with Windows 2000, if at least 4 files or folders already exist with the same initial 6 characters in their short names, the stripped LFN is instead truncated to the first 2 letters of the basename (or 1 if the basename has only 1 letter), followed by 4 hexadecimal digits derived from an undocumented hash of the filename, followed by a tilde, followed by a single digit, followed by a period ".", followed by the first 3 characters of the extension." – Maksym Aug 19 '11 at 09:50
  • 1
    I see but how likely you'll have at least 4 files with similar names? http://en.wikipedia.org/wiki/8.3_filename#How_to_convert_a_long_filename_to_a_short_filename Considering his example and following the pseudo algorithm described here, I suggest you try and see if your example resolves to the short format successfully. By the way, why do you need such function? I see that you're developing an AIR application using File system. Maybe, I can be more helpful if I know what you're really trying to achieve. Thanks. – Kumsal Obuz Aug 22 '11 at 14:31
1

Here's my handy method that does this below:

public static string GetShortPathName(string path)
{
    string[] arrPath = path.Split(System.IO.Path.DirectorySeparatorChar);
    path = arrPath[0];   // drive
    // skip first, ( drive ) and last program name
    for (int i = 1; i < arrPath.Length - 1; i++)                
    {
        string dosDirName = arrPath[i];
        if (dosDirName.Count() > 8)
        {
            dosDirName = dosDirName.Substring(0, 6) + "~1";
        }
        path += System.IO.Path.DirectorySeparatorChar + dosDirName;
    }
    // include program name if any
    path += System.IO.Path.DirectorySeparatorChar + arrPath[arrPath.Length - 1];   
    return path;
 }