Need to get 8.3 path on Windows

P

Patrick L. Nolan

Our python script uses popen to run an application on Windows XP.
The application chokes if the filename given as a command line
argument contains any spaces. It's happy with the 8.3 shortname
notation, though. So given a file with a name like
c:\Documents and Settings\Joe User\Desktop\My Files\foo.dat
I would like to be able to convert this to
c:\DOCUME~1\JOEUSE~1\Desktop\MYFILE~1\foo.dat

This would be a fairly simple exercise in text manipulation
except that the number may not always be 1. If the directory
names are not unique in the first 6 characters, there may
be ~2 etc.

Has this been solved by someone else?
 
M

Michael Geary

Patrick said:
Our python script uses popen to run an application on Windows XP.
The application chokes if the filename given as a command line
argument contains any spaces. It's happy with the 8.3 shortname
notation, though. So given a file with a name like
c:\Documents and Settings\Joe User\Desktop\My Files\foo.dat
I would like to be able to convert this to
c:\DOCUME~1\JOEUSE~1\Desktop\MYFILE~1\foo.dat

This would be a fairly simple exercise in text manipulation
except that the number may not always be 1. If the directory
names are not unique in the first 6 characters, there may
be ~2 etc.

You're looking for the Windows API function GetShortPathName, which is
supported by the win32api module in Mark Hammond's Windows extensions:

This function returns the actual short name for a file or directory; it
doesn't just do a string manipulation.

-Mike
 
D

Dennis Lee Bieber

Patrick L. Nolan fed this fish to the penguins on Friday 16 January
2004 23:09 pm:
Our python script uses popen to run an application on Windows XP.
The application chokes if the filename given as a command line
argument contains any spaces. It's happy with the 8.3 shortname

Did you try putting a set of quotes around the file name that is
passed to the application?

Even Windows file associations need the quotes in the command line.

--
 
M

Mike Huffman

import win32api
Definitely the way to go *IF* you know the user will have Windows
Extensions installed. However, if you can only count on a "standard"
Python installation you can use the system FOR command on Windows
2K/XP:

import sys, os

if os.environ['OS'] != 'Windows_NT':
print sys.argv[0], 'requires Windows 2000 or Windows XP'
sys.exit(1)

long_name = 'C:\\Documents and Settings\\All Users\\Start Menu' +
'\\Windows Update.lnk'
print '%-16s%s' % ('long_name: ', long_name)

for_cmd = 'for %I in ("' + long_name + '") do echo %~sI'
print '%-16s%s' % ('for_cmd: ', for_cmd)

p = os.popen(for_cmd)
short_name = p.readlines()[-1] # last line from for command

if p.close():
print 'Error calling shell command "for"'
else:
print '%-16s%s' % ('short_name: ', short_name)

If you need to support Windows 98 let me know; I have a script
(VBScript) that converts long names to short names using Windows
Script Host which I call with popen() as above. Maybe someone has a
better way to do it for Win98 without Python Win32 extensions.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top