473,480 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unicode-aware file shortcuts in Windows

Does anyone know how to create file shortcuts in Windows?

The only way I know is like:

---------------------------------------------------------------

import win32com.client

wScriptShellObject = win32com.client.Dispatch("WScript.Shell")
shortcutName = unicode("shortcut.lnk", "utf8")
shortcut = wScriptShellObject.CreateShortcut(shortcutName)
shortcut.TargetPath = <FULL PATH TO TARGET FILE>
shortcut.Save()

---------------------------------------------------------------

Unfortunately, this causes problems with Unicode characters:

(1) If there are Unicode characters in target file name, not all of them
get correctly copied to the shortcut's "target" attribute, thus
producing an invalid shortcut.

(2) If there are Unicode characters in shortcut name, not all of them
get correctly copied, thus producing a shortcut with a name different
than desired.

(3) If there are Unicode characters somewhere in the path to the
shortcut itself (above the shortcut name component), then they also get
corrupted, which effects in an invalid (non-existent) path and errors
like this one:

---------------------------------------------------------------

E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1>cf.py
Traceback (most recent call last):
File "E:\Documents and
Settings\Staszek\Progs\Python-Windows\test_1\cf.py", line 7, in ?
shortcut.Save()
File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
'WshShortcut.Save', 'Unable to save shortcut "E:\\Documents and
Settings\\Staszek\\Progs\\Python-Windows\\test_1\\Te\x9a\xed me c\xfd
z\xf3lazcseL\\link do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0,
-2147024893), None)

---------------------------------------------------------------

The original path to shortcut file referenced in error description above
contains Polish and Czech characters, and it was:

---------------------------------------------------------------

E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1\Těš* mě čý
żółąźćśęŁ\link do večer ŻÓŁĄKę ěš* bla.lnk

---------------------------------------------------------------

As it can be seen, some of those national characters in the error
description string are escaped using the \x.. sequence, while others are
simply "flatten" (like first "e" in "Te\x9a\xed", or "lazcse").

So it seems as if WScript.Shell object in Python didn't fully support
Unicode?... Or perhaps it does, but one has to know how to activate it.

Any help would be appreciated.

I am using standard Windows console cmd.exe in Windows 2000, Python 2.4,
and Mark Hammond's Python for Windows Extensions build 204.

+-------------------------------------------------------+
| When replying, please replace "my_initials" in the |
| From: address field with my initials - that is, "SF". |
+-------------------------------------------------------+

--
http://www.nglogic.com
Enter through the narrow gate! (Mt 7:13-14)
Sep 16 '05 #1
3 5613
Stanislaw Findeisen:
E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1>cf.py
Traceback (most recent call last):
File "E:\Documents and
Settings\Staszek\Progs\Python-Windows\test_1\cf.py", line 7, in ?
shortcut.Save()
File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
'WshShortcut.Save', 'Unable to save shortcut "E:\\Documents and
Settings\\Staszek\\Progs\\Python-Windows\\test_1\\Te\x9a\xed me c\xfd
z\xf3lazcseL\\link do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0,
-2147024893), None)


I see similar problems using WSH so I think the problem is in
WScript.Shell, rather than in Python's access to COM. Slightly
simplified and with the directory created:

<package>
<job id="js">
<script language="JScript">
WScript.Echo("Start")
var WshShell = WScript.CreateObject("WScript.Shell");
var oShellLink = WshShell.CreateShortcut("C:\\Těš* mě čý
żółąźćśęŁ\\link do večer ŻÓŁĄKę ěš* bla.lnk");
oShellLink.TargetPath = "c:\\bin\\bb.api";
oShellLink.Save();
WScript.Echo("End")
</script>
</job>
</package>

C:\bin>cscript ul.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Start
C:\bin\ul.wsf(9, 11) WshShortcut.Save: Unable to save shortcut "C:\Teš*
me cý zólazcseL\link do vecer ZÓLAKe eš* bla.lnk".
C:\bin>dir c:\T*
Volume in drive C has no label.
Volume Serial Number is A04B-224D

Directory of c:\

02/06/2005 10:30 PM 67 test.py
17/09/2005 09:56 AM <DIR> Těš* mě čý żółąźćśęŁ
1 File(s) 67 bytes
1 Dir(s) 7,014,309,888 bytes free

C:\bin>dir "c:\Těš* mě čý żółąźćśęŁ"
Volume in drive C has no label.
Volume Serial Number is A04B-224D

Directory of c:\Těš* mě čý żółąźćśęŁ

17/09/2005 09:56 AM <DIR> .
17/09/2005 09:56 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 7,014,309,888 bytes free

C:\bin>

Happy googling.

Neil
Sep 17 '05 #2

"Stanislaw Findeisen" <my*********@nglogic.com> wrote in message
news:dg**********@achot.icm.edu.pl...
Does anyone know how to create file shortcuts in Windows?

The only way I know is like:

---------------------------------------------------------------

import win32com.client

wScriptShellObject = win32com.client.Dispatch("WScript.Shell")
shortcutName = unicode("shortcut.lnk", "utf8")
shortcut = wScriptShellObject.CreateShortcut(shortcutName)
shortcut.TargetPath = <FULL PATH TO TARGET FILE>
shortcut.Save()

---------------------------------------------------------------


I see that another way is available here:
http://msdn.microsoft.com/library/de...s/shortcut.asp
I haven't tried, and I don't have the knowledge to convert the C++ to
Python, but it works at a lower level and may get around the bugs. It does
seem that the name of the file isn't Unicode in their method, either, so I'm
not sure if it'll work.
Sep 17 '05 #3
John Bauman wrote:
I see that another way is available here:
http://msdn.microsoft.com/library/de...s/shortcut.asp
I haven't tried, and I don't have the knowledge to convert the C++ to
Python, but it works at a lower level and may get around the bugs. It does
seem that the name of the file isn't Unicode in their method, either, so I'm
not sure if it'll work.


There are two versions of the IShellLink interface: IShellLinkA and
IShellLinkW. You need to use the *W version to pass characters outside
the ANSI code page.

Regards,
Martin
Sep 17 '05 #4

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

Similar topics

30
2711
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up...
8
3639
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary...
6
2758
by: S. | last post by:
if in my website i am using the sgml { notation, is it accurate to say to my users that the site uses unicode or that it requires unicode? is there a mathematical formula to calculate a unicode...
4
3151
by: Basil | last post by:
Hello. I have compiler BC Builder 6.0. I have an example: #include <strstrea.h> int main () { wchar_t ff = {' s','d ', 'f', 'g', 't'};
4
6034
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
2607
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
32
49646
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
6
5586
by: peter pilsl | last post by:
postgres 7.3.2 I store unicode-data in postgresql. The data is retrieved via webinterfaces, processed with perl and then stored in postgresql (and viceversa). All is going nice with one...
13
3260
by: Toms | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
24
8985
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
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...
0
7091
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...
0
6966
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...
0
5344
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 projectplanning, coding, testing,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.