如何通过文件夹重复并显示相对文件夹路径?
这是一个简单的脚本:
$srcpth = "C:\Users\Mark\Desktop\dummy\"
$files = Get-ChildItem -Path $srcpth -File -Recurse
foreach ($f in $files) {
$filen = $f.Name
$filesize = $f.Length
Write-Output "$filen $filesize"
}
这将正确循环循环 c:\ users \ mark \ desktop \ dummy
和带有文件大小的输出文件名,但它不会显示相对路径。如何解决相对路径?谢谢。
编辑:下面添加以澄清所需的输出:
例如,在 c:\ users \ mark \ desktop \ dummy
下,是带有文件
C:\Users\Mark\Desktop\dummy\file00.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_01.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_02.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_03.txt
C:\Users\Mark\Desktop\dummy\folder02\file02_01.txt
C:\Users\Mark\Desktop\dummy\folder02\file02_01.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_01.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_02.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_03.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_04.txt
输出的子文件夹,并产生上述代码:
file00.txt 9
file01_01.txt 10
file01_02.txt 12
file01_03.txt 12
file02_01.txt 15
file02_01.txt 14
file03_01.txt 11
file03_02.txt 15
file03_03.txt 13
file03_04.txt 12
但是我想要的是:
file00.txt 9
\folder01\file01_01.txt 10
\folder01\file01_02.txt 12
\folder01\file01_03.txt 12
\folder02\file02_01.txt 15
\folder02\file02_01.txt 14
\folder03\file03_01.txt 11
\folder03\file03_02.txt 15
\folder03\file03_03.txt 13
\folder03\file03_04.txt 12
先前的 \
,no slash或。\
很好。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您输出对象而不是像现在这样做的那样而不是字符串,无论如何,您可以使用
.substring(..)
或者您使用 > PowerShell Core ,您可以访问.net api
path.getRelativePath(String,string)
:还有< a href =“ https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.automation.automation.pathintrinsics.normalizarizerelative = wiew = powershellsdk-7.0.0.0.0.0 pathintrinsics.normalizerelative Path(String,String)方法 Windows PowerShell和PowerShell Core都可用
I would recommend you to output objects instead of strings as you're doing right now, in any case, you can get the relative paths either using
.SubString(..)
:Or if you're using PowerShell Core, you can access the .NET API
Path.GetRelativePath(String, String)
:There is also
PathIntrinsics.NormalizeRelativePath(String, String)
Method available to both, Windows PowerShell and PowerShell Core, though this seems an overkill:您可以在这里:
没有任何您要寻找的值的对象属性。但是您可以像上面的那样计算它。当您试图弄清楚这样的东西时,查看对象的成员总是很有用的:
这将使您可以更好地了解可以使用什么,可以使用哪些属性以及可用的方法。
Here you go:
There aren't any object properties with the value you're looking for. But you can calculate it like above. It's always useful to look at the members of an object when you're trying to figure something like this out:
This will give you a better idea of what you can work with, what properties you can use, and what methods are available.
而
string.substring()
/.remove()
和[io.path] :: getRelativePath()
解决方案在使用时就足够了仅绝对本地路径,当-path
get-childitem
的参数是 em avery 路径或a时,它们会失败。 powershell-folly-em>路径(请参阅本答案结束时的示例,以了解如何失败)。对于另外支持PowerShell路径和相对路径的解决方案,我建议使用
resolve-path-relative
:输出:
模式的模式故障:
这是
string.substring()
方法在我的系统上,在我系统上的powershell路径失败(您可能会看到不同的结果,取决于临时的位置目录):这就是
[io.path] :: getRelativePath()
失败:While the
String.Substring()
/.Remove()
and[IO.Path]::GetRelativePath()
solutions are sufficient when working with only absolute native paths, they fail when the-Path
argument forGet-ChildItem
is a relative path or a PowerShell-only path (see examples at the end of this answer for how they can fail).For a solution that additionally supports PowerShell paths and relative paths, I recommend to use
Resolve-Path -Relative
:Output:
Modes of failure:
This is how the
String.Substring()
method fails for the PowerShell path of the sample above, on my system (you may see a different outcome depending on the location of your temp directory):And this is how
[IO.Path]::GetRelativePath()
fails: