如何在PowerShell中解析当前目录下超259字符的文件路径?
问题描述
免费Steam游戏《索尼克谋杀案》(The Murder of Sonic The Hedgehog)中存在一个39KB的文件,文件名为1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle,其所在路径为:
F:\Program Files (x86)\Steam\steamapps\common\Themurderofsonicthehedgehog\The Murder of Sonic The Hedgehog\The Murder of Sonic The Hedgehog_Data\StreamingAssets\aa\StandaloneWindows64
由于该路径总长度超过Windows传统的259字符限制,只有在路径前加上\\?\前缀时,Resolve-Path -LiteralPath才能正常执行,示例如下:
$longPath = "\\?\F:\Program Files (x86)\Steam\steamapps\common\Themurderofsonicthehedgehog\The Murder of Sonic The Hedgehog\The Murder of Sonic The Hedgehog_Data\StreamingAssets\aa\StandaloneWindows64\1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle" Resolve-Path -LiteralPath $longPath
现在希望能仅通过指定文件名来解析该文件路径,比如先切换到目标目录再执行Resolve-Path:
cd "F:\Program Files (x86)\Steam\steamapps\common\Themurderofsonicthehedgehog\The Murder of Sonic The Hedgehog\The Murder of Sonic The Hedgehog_Data\StreamingAssets\aa\StandaloneWindows64\" Resolve-Path -LiteralPath 1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle
但执行上述代码会报错:
Resolve-Path : Cannot find path '1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle' because it does not exist. At line:1 char:1 + Resolve-Path -LiteralPath 1f3e56ae019cd278e6abd86e2a64eb7c_unitybuilt ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (1f3e56ae019cd27...b159399c.bundle:String) [Resolve-Path], ItemNotFoundE xception + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand
解决方案
当前目录本身属于长路径(超过259字符),PowerShell的cd命令虽能切换到该目录,但后续相对路径操作仍受传统路径长度限制,因此需要通过以下方式处理:
方法一:给当前目录添加\\?\前缀后拼接文件名
先获取当前目录的完整路径并加上长路径前缀,再拼接文件名执行解析:
# 切换到目标目录 cd "F:\Program Files (x86)\Steam\steamapps\common\Themurderofsonicthehedgehog\The Murder of Sonic The Hedgehog\The Murder of Sonic The Hedgehog_Data\StreamingAssets\aa\StandaloneWindows64\" # 构造带长路径前缀的完整文件路径 $longDirPath = "\\?\$($PWD.Path)" $targetFile = Join-Path -Path $longDirPath -ChildPath "1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle" # 解析路径 Resolve-Path -LiteralPath $targetFile
方法二:用Get-ChildItem结合长路径前缀查找文件
无需切换目录,直接指定带\\?\前缀的目录路径,筛选目标文件名:
$longDirPath = "\\?\F:\Program Files (x86)\Steam\steamapps\common\Themurderofsonicthehedgehog\The Murder of Sonic The Hedgehog\The Murder of Sonic The Hedgehog_Data\StreamingAssets\aa\StandaloneWindows64" $targetFile = Get-ChildItem -LiteralPath $longDirPath -Filter "1f3e56ae019cd278e6abd86e2a64eb7c_unitybuiltinshaders_85695fb1f6d830835b9e5284b159399c.bundle" # 输出文件完整路径 $targetFile.FullName
方法三:启用全局长路径支持(Windows 10 1607+/Windows Server 2016+)
若系统版本符合要求,可通过修改设置全局启用长路径支持,后续无需手动添加\\?\前缀:
- 注册表方式:打开注册表编辑器,定位到
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem,将LongPathsEnabled的值设为1(不存在则新建DWORD值)。 - 组策略方式:打开本地组策略编辑器(
gpedit.msc),依次展开计算机配置 > 管理模板 > 系统 > 文件系统,启用启用 Win32 长路径策略。
设置完成后重启PowerShell,即可直接执行你原本的代码。
内容的提问来源于stack exchange,提问作者Carly314
相关产品推荐
相关产品推荐

