473,795 Members | 3,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\JOE USE~1\Desktop\M YFILE~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 4299
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\JOE USE~1\Desktop\M YFILE~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 GetShortPathNam e, which is
supported by the win32api module in Mark Hammond's Windows extensions:
import win32api
win32api.GetSho rtPathName( "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.netc om.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.GetSho rtPathName( "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_cm d)
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
8594
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 running XP on the one where the issue arises). I can't figure out why the 'C:\WINDOWS\System32\python23.zip' entry is inside sys.path. That file/dir does not exist on the disk. Could a failing (crashing) Pythonwin installation caused that? ...
5
1762
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 $PATH / %PATH%, etc) but I am not able to find a simple function to do so. os.environ.split(os.sep) is wrong on Windows for the case when PATH="c:\\A;B";c:\\D; where there is a ';' embedded in the quoted path.
23
8951
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 the remove function that is included with <cstdio> but cannot get it to work properly. My reference book has the following....
6
3432
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? >------------- >Python could not import the service's module > File "T:\Python\Projects\NamedConnector\Service.py", line 17, in ? > from Processor import * > File "c:\Python\Projects\NamedConnector\Processor.py", line 35, in ?
0
3003
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 following command line: caspol.exe -polchgprompt off -machine -addgroup 1 -url "file://<UNC path to dll>\mixedMode.dll" FullTrust ame "GroupName" -polchgprompt on
4
1888
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 different forum if I made a mistake. In any case, thank you for spending the time to read this loooong case and/or for your input. With that said, here's the background of the situation:
3
2096
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 windows platform as the most relevant in this case. os.access() doesn't handle env PATHEXT and can't detect if a given path would be executable or not. Here "executable" means file that
11
26643
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 and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my server shows the c: \php5\pear path. I can't change it no matter what I do I also tried the...
1
2287
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 it off of my machine every once in a while. It works perfectly. I built it and sent it to my co worker's machine to test it out and it returns an "Illegal characters in path" error when the line: Dim objReaderOpen1 As New...
12
8604
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 the program's directory. What I need to do is read that file regardless of where (cwd) I execute the program from, without hardcoding the absolute since the program might be moved or be in differant locations on other computers.
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.