By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Unicode-aware file shortcuts in Windows

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.