How to convert long Win95 paths to 8.3 dos paths?

Tim Peters tim_one at email.msn.com
Thu May 6 23:04:42 EDT 1999


[Michael Scharf]
> I am looking for a function, that can convert
> long NT or Win95 names to 8.3 dos names
> (the same name that 'dir /x' would show).
> I could not find anything relevant in the
> with dejanews nor on the python pages...

Heh.  Heh heh.  Here's an extract from
    http://www.zdjournals.com/ddj/9608/ddj9681.htm


    You might expect to find [an MS API] function that will perform
    the necessary conversions from a long filename to an alternate
    filename and back. Surprisingly, the Windows 95 API doesn't supply
    such a function.

    Instead, Microsoft suggests that you call the FindFirstFile() API
    function, which initializes a data structure with both the long and
    alternate filenames for a given file.  The reasoning behind this
    approach is that calling the FindFirstFile() function guarantees
    that the file exists. ... Unfortunately, the resulting data
    structure contains only the long and alternate names of the file,
    not the pathname.

    ...

> This would convert
>   C:\Program Files
> to
>   C:\PROGRA~1

That's part of the problem, of course:  the turd after the tilde depends on
how many other files/directories began with the same prefix at the time the
file of interest was created.  That is, "the" correct encoding of a long
name is an illusion:  a long name may map to different short names across
Windows installations.

So you need to install the Win32 extensions and do the FindFirstFile
business, or feed some form of "dir" (note that /x is not a legit dir option
under Win95) to  an os.popen call, and parse the output.

all-long-obvious-to-the-most-casual-short-observer-ly y'rs  - tim






More information about the Python-list mailing list