我正在做一个跨平台的项目,想通过PowerShell处理一些可以支持多平台的路径。
比如我需要从Windows的NTFS传输一串路径到Linux,通过挂载NTFS从Linux访问该路径,并处理该路径用于其他正常用途。
程序可以如下:
问题是:
c:\uSeRs\usER\TesT.tXt
,即使它代表真实的路径C:\Users\User\test.txt
c:\uSeRs\usER\TesT.tXt
和 C:\Users\User\test.txt
,因为它们都代表相同的资源。Explorer.exe
看到路径的真实情况,这意味着原始区分大小写的路径的信息只是记录在某处。从区分大小写的路径中,我们可以定位到特定的项目,但是如何获取包含其区分大小写的路径的原始信息?可能点数太多。但我的关键问题可以描述为以下目标:
C:\Users\User\test.txt
。c:\uSeRs\usER\TesT.tXt
C:\Users\User\test.txt
,我们可以从explorer.exe
我已经尝试过
Get-Item
,Get-ItemProperty
和类似这里的东西,但它们不起作用:
#Requires -Version 7.0
$target = 'C:\Users\User\test.txt'
New-Item -Path $target -ItemType File
$known = 'c:\uSeRs\usER\TesT.tXt'
(Get-Item $known).FullName
# C:\uSeRs\usER\TesT.tXt
# 'c' has been converted to `C` but the other parts have not been converted to the target
(Get-Item $known).FullName -eq $target
# False
# see https://stackoverflow.com/a/48845935/17357963
(Get-Item $known).Target
# return $null or empty
(Get-ItemProperty $known).FullName -eq $target
# False
(Get-ItemProperty $known).Target
# return $null or empty
提前致谢。
根据评论,我们得出的答案是:
Get-ChildItem (Split-Path $known) | Where-Object Name -eq (Split-Path $known -leaf)).FullName