Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert long Win95 paths to 8.3 dos paths?

20 views
Skip to first unread message

Tim Peters

unread,
May 6, 1999, 3:00:00 AM5/6/99
to pytho...@python.org
[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

Christian Tismer

unread,
May 7, 1999, 3:00:00 AM5/7/99
to

Tim Peters wrote:
>
> [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.

Well, it has to be done step by step using FindFile.
Here a Python version.

ciao - chris

--- BEGIN shrtname.py ---
import win32api, string

def LongToShort(fname):
pieces = string.split(string.replace(fname, "/", "\\"), "\\")
if not pieces: return ""
pre = pieces.pop(0)
if not pre or pre[-1:]==":" : # abs path
pre = pre + "\\"
if not pieces: return ""
first = pieces.pop(0)
else: # rel path
parts = string.split(pre, ":", 1)
if len(parts) > 1:
pre = parts[0] + ":"
first = parts[1]
else:
first = pre
pre = ""
try:
oldpath = pre+first
newpath = pre+_get_short(oldpath)
for piece in pieces:
oldpath = oldpath + "\\" + piece
newpath = newpath + "\\" + _get_short(oldpath)
return newpath
except TypeError: return "" # we got none for the short path

def _get_short(longpath):
dir = win32api.FindFiles(longpath)
if dir:
short = dir[0][-1]
if not short:
short = dir[0][-2] # was a true short one
return short

---END shrtname.py ---

--
Christian Tismer :^) <mailto:tis...@appliedbiometrics.com>
Applied Biometrics GmbH : Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101 : *Starship* http://starship.python.net
10553 Berlin : PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint E182 71C7 1A9D 66E9 9D15 D3CC D4D7 93E2 1FAE F6DF
we're tired of banana software - shipped green, ripens at home

Brad Clements

unread,
May 7, 1999, 3:00:00 AM5/7/99
to

Tim Peters wrote in message <000501be9836$5aad10e0$979e2299@tim>...

>[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.
>


Funny, one of my Delphi programs does this..

Given a long file name pointed to by ES:DI, point DS:SI to the address of
the result buffer, set CX=1, AX=0x7160 and call int 21.

If carry is not set on return, then the result buffer has the short name.

To go the other way (short to long), set CX=2

Uh... this is a 16-bit program, your mileage will vary.. ;-)

0 new messages