簡體   English   中英

刪除文件全名的第一部分

[英]Remove first part of file fullname

這是18年5月18日的更新:感謝所有發布者。 每個人都分享了一些很棒的信息。 最終為我點擊后,我就能使用DirectoryName找出一個非常簡單的解決方案:

$file = Get-ChildItem -path "c:\temp\testc" -Recurse 
$destshare = "C:\temp\testD\"
$Dest = ($file.DirectoryName.replace($file.directoryname, $destshare))

原始帖子如下:

我正在使用Get-Childitem .fullname從文件中提取Get-Childitem並希望從源中刪除 server\\sharename

使用$f.fullname.split('\\)[X]保留我選擇的陣列部分,但是有沒有辦法刪除 \\\\server\\share$\\\\ECNFILE01\\santana-carlos$\\ )?

從:

\\ECNFILE11\Joe-smith-J$\work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx

我只想要:

\work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx

每個用戶的服務器\\共享名將不同: \\\\ECNFILE11\\Joe-smith-J$\\\\ECNFILE9\\user2$\\\\ECNFILE1\\user3等...

謝謝

您可以將字符串轉換為URI ,並利用HostSegments屬性以所需的\\\\server\\share\\樣式構建字符串:

$URI = [URI]"\\ECNFILE11\Joe-smith-J$\work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx"
Write-Output "\\$($URI.Host)\$($URI.Segments[1])"

> \\ecnfile11\Joe-smith-J$/

Windows將在UNC路徑中自動更正\\ vs / ,但是如果您希望的話,您始終可以使用replace來更改它。

Write-Output "\\$($URI.host)\$($URI.Segments[1] -replace('/','\'))"

> \\ecnfile11\Joe-smith-J$\

不使用\\\\server\\share返回路徑

$UNC = "\\ECNFILE11\Joe-smith-J$\work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx"
$URI = [URI]$UNC
$UNC -replace "^$([regex]::Escape("\\$($URI.host)\$($URI.Segments[1] -replace('/','\'))"))"

> work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx

您可以在Do循環中使用Split-Path ,直到它不返回任何內容,然后獲取先前的結果,該結果將成為最后一個有效的UNC路徑,例如\\\\server\\share 然后正則表達式從全名替換

$Files = @('\\ECNFILE11\Joe-smith-J$\work\O365 SOP - New User Account Creation BEANE EDITS and MARKUPS.docx','\\ECNFILE01\santana-carlos$\!Archive!\cotr\Task Orders\OPS\OPS-TO-09-1\OPS-TO-2009-1 Extension\OPS-TO-2009-1 MOD6\OPS-TO-2009-2 MOD6 InitiationForm.doc')
foreach ($File in $Files) {
    $PathParent = $File 
    do {
        $PreviousParent = $PathParent
        $PathParent = Split-Path $PathParent -Parent
    }
    while ($PathParent)
    $File -replace "^$([regex]::Escape(PreviousParent))"
}

要從網絡共享路徑中刪除\\\\servername\\sharename$ ,您只需采用Split('$\\')[1]

但是,如果共享不是管理共享(共享名末尾沒有$符號),則必須用'\\'分割,刪除前4行,在其余各行之間用\\聯接其余部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM