向本机命令提供文件路径时,Powershell 抛出“文件名或扩展名太长”错误

问题描述 投票:0回答:1

这是我多次遇到的问题,尤其是在使用基于 Python 构建的本机命令时。

例如今天,我有一系列图像(大约 700 张),我想获取这些图像并将它们传递给名为 img2pdf 的本机命令,以便输出单个 PDF 文档。

图像路径的片段是:

C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_1.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_2.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_3.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_4.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_5.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_6.png
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_7.png
...
C:\Users\ralf\Documents\raw scans\volume 39\chapter 1\Page_687.png

和命令:

$images = get-childitem -path 'C:\Users\ralf\Documents\raw scans\volume 39\chapter 1' | sort creation time
Img2PDF $images -o "chapter1.pdf"

但是上面的方法失败了:

ResourceUnavailable: Program 'img2pdf.exe' failed to run: An error occurred trying to start process 'C:\Users\ralf\AppData\Local\Programs\Python\Python311\Scripts\img2pdf.exe' with working directory 'C:\Users\ralf\Documents\raw scans\volume 39\chapter 1'. The filename or extension is too long.At line:1 char:1

如果我只传递前 5 个图像的路径,该命令将起作用并生成文档。有时,我什至通过

让它发挥作用
  • 将图像放置在
    c:\temp\Page_1.png
    c:\temp\Page_2.png
  • 然后调用
    image2pdf
    并传递新路径
  • 最后将它们移回原来的位置 但这并不理想,有时我会搞砸得很厉害,必须清理它。

很多次我试图解决这个问题,一旦我按照这个指南并在我的注册表中启用了

LongPathsEnabled
,但重新启动后,这没有任何效果。现在甚至已启用。

这个问题在最关键的时候不断出现,我真的很想处理一次,感谢您的帮助。

powershell
1个回答
0
投票

我想我有一个简单的解决方案供您尝试。将驱动器映射到您的文件夹,根据映射的驱动器处理文件,删除映射。

$tmpDrive = New-PSDrive -Name G -PSProvider FileSystem -Root 'C:\Users\ralf\Documents\raw scans\volume 39\chapter 1'
$images = get-childitem -path G:\ | sort LastWriteTime
Img2PDF $images -o "chapter1.pdf"
$tmpDrive | Remove-PSDrive
© www.soinside.com 2019 - 2024. All rights reserved.