Thread: Windows 2k/2k3/xp Long Filename Truncation

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Windows 2k/2k3/xp Long Filename Truncation

    Hey everybody, I'm having a small problem:

    Truncating long Windows filenames to DOS-compilant 8+3 filenames is easy, as long as you don't have more than 4 similar files - take the first 6 characters, add ~1(2,3,4) plus the extension and you're done.

    So far, so good.

    My problem right now is Window's way of truncating the filenames when you have more than four similar files, since then, starting with the 5th file, Windows takes only the first two letters and then adds some sort of hash (4 characters long) and the ~1 to it, it doesn't just increment the number at the end like with the first four.

    Now, Windows comes with a handy little function called GetShortFileName you can use to generate the 8+3 name of an existing file, but you can't use it to generate the 8+3 name of a file that doesn't exist on your harddisk, if you call it with a non-existing path as the argument, it just returns.

    So you can see where this is going, does anyone know how this "hash" is generated?
    Microsoft itself is pretty quiet about that, all I could find is a short paragraph in an MSPress book ([LINK]), but that just states that it is done the way I described it, it doesn't explain how that hash is generated, which is what I'm interested in :/

    And that's it, I'm at a loss what to do, so any help is really appreciated.

    If you want to check for yourself, create 5 Files with similar names (LongFileNameOne.txt, LongFileNameTwo.txt, etc.), then do a "dir /x" and watch the result.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean
    Code:
    longfi~1
    longfi~2
    longfi~3
    longfi~4
    ...
    longf~33
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Why not just create the file, then get the short file name, then delete it again?

    *edit*DWKS: if you try what he says, you see that the file names don't end up looking like that, copy of text document.txt, turns into co0dfb~1.txt for example.
    Last edited by Dweia; 12-14-2005 at 11:15 PM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The reason GetShortFileName doesn't work with non-existent files is that it doesn't generate the file name, it just reads the one stored in the filesystem.
    Can't help you with your problem, sorry.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    *edit*DWKS: if you try what he says, you see that the file names don't end up looking like that, copy of text document.txt, turns into co0dfb~1.txt for example.
    Is that right? On my Windows 98 computer, all unacceptable characters get removed and a number is added to the end and the extension is truncuated:
    Code:
    "Copy of hello.text" -> "copyof~1.tex"
    "Copy of hippo.text" -> "copyof~2.tex"
    "h.long" -> "h~1.lon"
    "dxcvbn001.l" -> "dxcvbn~1.l"
    "dxcvbn002.l" -> "dxcvbn~2.l"
    ...
    "dxcvbn010.l" -> "dxcvb~10.l"
    ...
    "dxcvbn100.l" -> "dxcv~100.l"
    I'm not sure what happens to a file with a tilde in the name.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  4. Easiest what to get a windows handle with just the filename?
    By no-one in forum Windows Programming
    Replies: 3
    Last Post: 06-26-2002, 07:24 PM
  5. Displaying LONG filename on limited static control
    By Garfield in forum Windows Programming
    Replies: 5
    Last Post: 01-30-2002, 07:09 PM