473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need to get 8.3 path on Windows

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?

--
* Patrick L. Nolan *
* W. W. Hansen Experimental Physics Laboratory (HEPL) *
* Stanford University *
Jul 18 '05 #1
3 4259
Patrick L. Nolan wrote:
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:
import win32api
win32api.GetShortPathName( "C:\Program Files\Internet Explorer" ) 'C:\\PROGRA~1\\INTERN~1'


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

-Mike
Jul 18 '05 #2
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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #3
> >>> import win32api
win32api.GetShortPathName( "C:\Program Files\Internet Explorer" ) 'C:\\PROGRA~1\\INTERN~1'


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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pierre Rouleau | last post by:
I can't remember where/how sys.path is set (aside from the automatically loaded site.py) and i get a strange entry in it. Can anyone remind me where/how to control sys.path on a Win32 machine (i'm...
5
by: chirayuk | last post by:
Hi, I am trying to treat an environment variable as a python list - and I'm sure there must be a standard and simple way to do so. I know that the interpreter itself must use it (to process...
23
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
6
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32 service? Is there any way to make it working? ...
0
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the...
4
by: John Smith | last post by:
Hello, I'm not sure if these are the appropriate forums for my question since it is closer to about Visual Studio 2005 than it is about .NET framework. So please pardon me and direct me to a...
3
by: techtonik | last post by:
Hello, everyb. Does anybody know simple cross-platform method of probing if executable binary is available and launching it. Problem no.1: test if executable file is available I'll take...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
1
by: crusson | last post by:
(edited to add): this is in Visual Basic .net I am at a complete loss... I've been building a program on my machine, running it out of the developer with the f5 key and builidng it and running...
12
by: =?ISO-8859-1?Q?Thomas_B=F8rlum?= | last post by:
Hey all, I'm writing a c++ program that needs to read a file. I'm trying to read a file that is in the same directory as the executable. Everything works fine if I execute the program while in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.