win32api -- long vs short filenames 
Author Message
 win32api -- long vs short filenames

I'm trying to expand a mangled DOS 8+3 name (from WinNT4.0) into the long
version of the name. There's a win32api function for doing the opposite, for
example,

Quote:
>>> win32api.GetShortPathName(r'c:\long_path_name\long_file_name.txt')

'c:\\LONG_P~1\\LONG_F~1.TXT'

But I can't figure out how to expand a mangled name into the original long
name. Any ideas?

--David Niergarth



Sun, 20 Jan 2002 03:00:00 GMT  
 win32api -- long vs short filenames

Quote:

> I'm trying to expand a mangled DOS 8+3 name (from WinNT4.0) into the long
> version of the name. There's a win32api function for doing the opposite, for
> example,

> >>> win32api.GetShortPathName(r'c:\long_path_name\long_file_name.txt')
> 'c:\\LONG_P~1\\LONG_F~1.TXT'

> But I can't figure out how to expand a mangled name into the original long
> name. Any ideas?
>>> win32api.FindFiles(r'c:\PROGRA~1')[0][-2]

'Program Files'

See doc in the .hlp file.

--david



Sun, 20 Jan 2002 03:00:00 GMT  
 win32api -- long vs short filenames


Quote:
> I'm trying to expand a mangled DOS 8+3 name (from WinNT4.0) into the long
> version of the name. There's a win32api function for doing the opposite,
for
> example,

> >>> win32api.GetShortPathName(r'c:\long_path_name\long_file_name.txt')
> 'c:\\LONG_P~1\\LONG_F~1.TXT'

> But I can't figure out how to expand a mangled name into the original long
> name. Any ideas?

> --David Niergarth

David Asher (thanks, by the way) pointed me to win32api.FindFiles(), as in

Quote:
>>> win32api.FindFiles(r'c:\PROGRA~1')[0][-2]

'Program Files'

This returns the long version of the last component of the path that you
feed it but not the long version of the entire path, which is what I'm
after. In case anybody is interested, here's a function that will return the
long version of the entire path from either a mangled absolute path or a
mangled simple file name in Python's current directory.

def win32_GetLongPathName(pathname):
    """Transform a mangled 8+3 pathname to original long name.

    pathname -- Can be an absolute path or a simple file or direcotry name
                if it exists in Python's notion of the current directory.

    If a simple file or directory name cannot be resolved or an absolute
    path cannot be found, the original pathname will be silently returned.

    """
    import os, win32api
    if os.path.isabs(pathname):
        abspath = pathname
    else:
        cur_dir = os.getcwd()
        newpath = os.path.join(cur_dir, pathname)
        if os.path.isfile(newpath):
            abspath = newpath
        else:
            return pathname  # could not resolve relative path
    pieces = []
    head, tail = os.path.split(abspath)
    while tail:
        try:
            longtail = win32api.FindFiles(os.path.join(head, tail))[0][-2]
        except IndexError:
            return pathname  # could not find path
        pieces.insert(0, longtail)
        head, tail = os.path.split(head)
    pieces.insert(0, head)
    return apply(os.path.join, pieces)

Seems like there should be an easier way, but this works for me.

--David Niergarth



Fri, 25 Jan 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. long = short*short ; (short,short) = long/short

2. long-to-short dir/filenames

3. Long Filenames okay for physical filenames???

4. DOS 8.3 filenames (how to remove long filenames header)

5. Library os.path.isfile(filename) - long filenames error.

6. Getting "Short" filenames inWindows

7. C++ vs Eiffel vs Smalltalk (warning--long language war)

8. LAHEY vs DEC vs MS-Powerstation (long)

9. C++ vs Eiffel vs Smalltalk (warning--long language war)

10. Osx long filenames truncated

11. VW2.0 under Win 95 + long filenames?

12. Long Filenames

 

 
Powered by phpBB® Forum Software