Windows 路径太长 [英] Windows path too long

查看:82
本文介绍了Windows 路径太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段显示了如何在 Windows 中提取快捷方式的目标:

Following snippet shows how to extract the target of a shortcut in Windows:

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
fp = r'C:\very_long_path\to\a\link\file\shortcut.lnk'
shortcut = shell.CreateShortCut(fp)
#targetPath = "\\\\?\\"+shortcut.Targetpath # Does not help
targetPath = shortcut.Targetpath

如果 lnk 文件在太长的路径上,上面的代码将失败.在这种情况下如何获取快捷方式的目标路径?

The code above fails if the lnk file is on a too long path. How can I get the shortcut's target path in this case?

推荐答案

我相信您面临的限制在 win32com.client.Dispatch("WScript.Shell") 这就是为什么您正面临 260 个字符的限制,并且以 \\?\ 为前缀并不能解决您的问题.

I believe the limitations you are facing are within the win32com.client.Dispatch("WScript.Shell") this is why you are facing the 260 character limitation and the prefixing with \\?\ is not solving your problem.

你必须在

  • 将文件夹复制到较短的路径(复制可以使用 \\?\前缀来避免路径长度的限制)
  • 然后创建快捷方式(没有 \\?\ 前缀),但使用更长的路径目标.
  • 并将所有内容复制回来(再次使用 \\?\ 前缀).
  • copying folder to a shorter path (the copying can be done with \\?\ prefix to avoid limitation of path length)
  • then create the shortcut (without \\?\ prefix), but with the longer path target.
  • and copy all back (again with \\?\ prefix).

或者,您可以考虑使用符号链接代替快捷方式(Windows Vista 及更高版本),后者可以使用 os.symlink()<创建/code> 并且这将与 \\?\ 前缀一起用于长路径名称.这是我个人多次做过的,因为它更容易实现.但是你必须问这个问题是否真的需要这个快捷方式"或符号链接",因为一旦你有一个符号链接",你需要在删除时将两个源作为符号链接删除,否则你的文件将保留.

Alternatively you can consider using a Symbolic Link in stead of a Shortcut (Windows Vista and higher) which can be created with os.symlink() and this will work with \\?\ prefix for long path names. This is what I personally have done multiple times, as it is just simpler to implement. But you will have to ask the question if this "shortcut" or "symlink" is really needed, as once you have a "symlink" you do need to delete both source as symlink upon delete, else your file will remain.

最后,您可以通过一种艰难的方式做到这一点:重新创建代码以创建正确格式的快捷方式文件.这里有一些微软文档,在线搜索会发现多个链接有用的信息.

Lastly you can do this the hard way: recreate code to create a shortcut file in the correct format. Here is some Microsoft Documentation, a search online will reveal multiple links with useful information.

虽然我没有这方面的经验,但似乎有一个 模块声称它可以本机创建 .lnk 文件

Although I do not have experience with this, there seems to be a module which claims it can natively create .lnk files

这篇关于Windows 路径太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆