拆分文件全名路径

时间:2014-06-23 15:26:14

标签: powershell

我想知道这是否可能,我试图错误地使用“split-Path”,我的文件名消失了。提前谢谢。

实际输出:

\\myHostname\c$\users\myUser\desktop -- 9,05 MB

Ficheros                                                                 Megas
--------                                                                 -----
\\myHostname\c$\users\myUser\Desktop\something.exe                       5,00
\\myHostname\c$\users\myUser\Desktop\photo.jpg                           2,00

期望的输出:

Ficheros                           Megas
--------                           ------
\Desktop\something.exe             5,00
\Desktop\photo.jpg                 2,00

CODE:

param([string]$pc,[string]$user)

$w7="\\$pc\c$\users\$user\desktop"

if(Test-Path $w7){

    #Desktop:
    $colItems = (Get-ChildItem $w7 | Measure-Object -ErrorAction "SilentlyContinue" -property length -sum) 
    $msg = "$w7 -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
    Write-host -ForegroundColor Green "$msg"

    #Files:
    Get-ChildItem -Recurse -force -Include *.* -exclude *.ini, *.lnk $w7 -ErrorAction "SilentlyContinue" | ls | Select-Object @{Name="Ficheros";Expression={$_.FullName}}, @{Name="Megas";Expression={$_.Length / 1MB}}


}

else{
    Write-Host -BackgroundColor White -ForegroundColor Red "FAIL"
}

1 个答案:

答案 0 :(得分:1)

你能试试吗?

Name="Ficheros";Expression={$_.FullName -replace '.*?(\\Desktop.*)','$1'}

编辑: 根据@TheMadTechnician

的建议更新非贪婪的正则表达式