header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Working with UNCs
Last Post 12 Oct 2011 04:25 AM by Bobdee. 22 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
BobdeeUser is Offline
Basic Member
Basic Member
Posts:130
Avatar

--
07 Oct 2011 08:10 AM
    Hi,

    I know it's basic PS, but I'm having some difficulty in removing some of a string.

    Basically, I will have a UNC path in a string which will include a filename - IE \c$\folder\thisfile.pdf - and I want to remove anything after the last '\'.

    I thought I could use .trim or something like it, but I don't know the amount of characters that I will be removing each time.

    I have to go through this route unfortunately because the data I am using is coming from another source and I am working with that.

    Can anyone point me in the right direction please?

    Thanks in advance,

    Bob.
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 08:18 AM
    Here is what I have already - I forgot to put it in the original post

    $string = "robbie\robbie\robbie.pdf"

    $a = @($string.Split('\') | Where-Object {$_ -like '*.*'})

    ($string -replace $a, '')

    The only problem I have with this, is that some directory names will include '.'s.  So it isn't fully compliant with my needs.

    Thanks in advance

    bob.
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    07 Oct 2011 08:19 AM
    There would be other ways...

    PS>"\c$\folder\thisfile.pdf".trim("\c$\folder\thisfile.pdf".split("\")[-1])
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 08:23 AM
    That's almost perfect Marco - thanks. It leaves me with the middle part of the UNC however.

    Thanks.
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    07 Oct 2011 08:26 AM
    Can you show a more complete example? I guess I don't understand.
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1094
    Avatar

    --
    07 Oct 2011 09:20 AM
    Whenever you have a string manipulation question, in general giving an input example and a desired output example will help.
    "Look Ma...no strings!"
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 09:35 AM
    Sure - apologies.

    So - $string = '\c$\folder\su-folder\file.pdf'

    And I'd like to reduce that to - '\c$\folder\sub-folder\'

    There may be more sub-folders, so I'd just like to remove the filename,

    Thanks
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1094
    Avatar

    --
    07 Oct 2011 09:45 AM
    • Accepted Answer
    With split:

    [string]::Join('\', $string.Split('\')[0..$($string.Split('\').Count-2)])

    With a regex:

    $string -replace '^(.*)\\.*$', '$1'
    "Look Ma...no strings!"
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    07 Oct 2011 09:55 AM
    Or...

    This also works as I had originally posted?

    $string.trim($string.split("\")[-1])
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 09:56 AM
    Awesome EB - thanks very much. It confused me tbh, but I understand what you've posted. Very appreciated.

    Thanks too Marco - apologies for not posting a desired result. (I think I have your book btw!)

    Thanks again fellas,

    Bob.
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    07 Oct 2011 10:03 AM
    PS > $string = '\c$\folder\su-folder\file.pdf'
    PS > split-path $string
    \c$\folder\su-folder

    PS > (split-path $string) + '\'
    \c$\folder\su-folder\

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1094
    Avatar

    --
    07 Oct 2011 10:05 AM
    You know I thought about Split-Path. I just wasn't sure if the leading \ would give it grief, then forgot to try and see.
    "Look Ma...no strings!"
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 12:02 PM
    Marco - you were right originally. I'm not at work now, so unless I log on from home (not a chance) to see why I found a different result earlier, I can't say why I didn't agree originally.

    Originally I found that your PS only gave me \folder\ so tried modifying the the PS, but no results came back as I hoped... I tried what you posted on my home machine and yes, you were completely right. The only thing I can think that might of caused it is that I was using sql to build the string...

    It's bothering me now. I will see from work.

    btw - thanks shay
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    07 Oct 2011 12:25 PM
    I dunno, it's the same thing in work - 2 different environments, using PowerGui and the shell - same result.

    Since it's 9.26pm, I'll leave it there. Thanks for the help tho guys.
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    10 Oct 2011 05:13 AM
    I have another query with this guys. If say, I wanted to break a unc down folder by folder, what would be the best method of doing this?

    I mean breaking it down like this :

    \\server\c$\folder1\folder2\folder3\folder4\
    \\server\c$\folder1\folder2\folder3\
    \\server\c$\folder1\folder2\
    \\server\c$\folder1\

    Basically, I want to start with a complete UNC that contains a file, then work backwards and check the contents of each directory that contains it... If that makes sense?

    Thanks

    Robbie.
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    10 Oct 2011 05:53 AM
    Not pretty, but works:

    $path="\\server\c$\folder1\folder2\folder3\folder4\"
    $tmp=$path

    for($i=$path.split("\").count;$i -gt 4;$i--){
    $tmp
    $tmp=$tmp|split-path
    }
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    10 Oct 2011 06:03 AM
    Brilliant! Pretty is not a concern :-)

    Thanks Marco
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    11 Oct 2011 08:58 AM
    Try the URI class:

    PS> $unc = [System.URI]"\\server\c$\folder1\folder2\folder3\folder4\"
    PS> $unc.Segments
    /
    c$/
    folder1/
    folder2/
    folder3/
    folder4/

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1094
    Avatar

    --
    11 Oct 2011 10:07 AM
    Show off.
    "Look Ma...no strings!"
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    11 Oct 2011 10:09 AM
    Actually, that's not exactly what is needed here, because one would have to re-combine the strings again to to be able to move through each directory.

    But it is cool overall...
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    11 Oct 2011 10:15 AM
    You could also use split-path a few times:

    $path "\\server\c$\folder1\folder2\folder3\folder4\"
    1..4 | foreach { $path = split-path $path; $path}

    \\server\c$\folder1\folder2\folder3
    \\server\c$\folder1\folder2
    \\server\c$\folder1
    \\server\c$

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1094
    Avatar

    --
    11 Oct 2011 11:20 AM
    Or more generically without knowing how many parts are in the path:

    $unc = [System.URI]"\\server\c$\folder1\folder2\folder3\folder4\"
    0..$($unc.segments.count-1) | %{[string]::Join('', $unc.segments[0..$($unc.segments.count-$_)])}

    /c$/folder1/folder2/folder3/folder4/
    /c$/folder1/folder2/folder3/folder4/
    /c$/folder1/folder2/folder3/
    /c$/folder1/folder2/
    /c$/folder1/
    /c$/
    "Look Ma...no strings!"
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    12 Oct 2011 04:25 AM
    All very cool thanks guys.  Although Marcos post worked originally, so I used it.  Here's what I finished with.  It's a script to remove anything that is not needed in particular paths complied from a SQL query, where there should only be 1 file per entry.  In some cases I found that the directory and sub-directories included files, so I had to work backwards from the original path and remove anything that was not part of the required UNC path.  Before I wrote this, we were taking of spending 5 working days removing 100s of GBs of un-needed data.  No longer!

    I know it's pretty basic PS, but it works perfectly which is the most important thing - right?  I've removed my comments as I know you'll like reading through it :-).

    And, any suggestions are very welcome.

    Thanks for the help,

    Bob.

    foreach ( $entry in $SQLQuery )
    {
    if ($entry.pathtofile -like '*\*') 
    {
    $currentPath = $entry.currentPath + (split-path $entry.pathtofile -parent) + '\'
    $containerPath = $entry.containerPath + (split-path $entry.pathtofile -parent) + '\'
    $pathtofile = Split-Path $entry.pathtofile -Leaf
    $tmp = $currentPath

    for( $i = $currentPath.split("\").count; $i -gt 7; $i-- ){ 
    $tmp = $tmp | split-path
    $tmp

    ##  Begin.
    [array]$subOne = Get-ChildItem $currentPath
    if ( $subOne.count -gt 1 )
    {
    Get-ChildItem $currentPath | select Name, FullName, @{Name="Mbytes";Expression={$_.Length / 1Mb}} | foreach { 

    if ($_.name -ne $pathToFile ) 
    if(!(Test-Path -Path $entry.containerPath))
    {
    Copy-Item $entry.currentPath -Destination $entry.containerPath -recurse
    }
    if(Test-Path -Path $entry.containerPath)
    {
    $value = $_.Fullname + ',' + $_.Mbytes
    Add-Content c:\temp\Content-Cleanup.csv -value $value
    Remove-Item $_.FullName -Recurse
    }
    }
    }
    }

    ##  Begin.
    [array]$subTwo = Get-ChildItem $tmp
    if ( $subTwo.count -gt 1 )
    {
    Get-ChildItem $tmp | select Name, FullName, @{Name="Mbytes";Expression={$_.Length / 1Mb}} | where { $_.FullName -notlike $r.TrimEnd('\') -and $_.FullName -ne $currentPath.TrimEnd('\') } | foreach { 
    if(!(Test-Path -Path $entry.containerPath))
    {
    Copy-Item $entry.currentPath -Destination $entry.containerPath -recurse
    }
    if(Test-Path -Path $entry.containerPath)
    {
    $value = $_.Fullname + ',' + $_.Mbytes
    Add-Content c:\temp\Content-Cleanup.csv  -value $value
    Remove-Item $_.FullName -Recurse
    }
    }
    }
    $r = $tmp
    }
    }
    else
    {
    $currentPath = $entry.currentPath
    $containerPath = $entry.containerPath
    $pathtofile = $entry.pathtofile
    $currentPath

    [array]$sub = Get-ChildItem $currentPath | where{!$_.PsIsContainer}

    ##  Begin.
    if ( $sub.count -gt 1 )
    {
    Get-ChildItem $currentPath | select name, FullName, @{Name="Mbytes";Expression={$_.Length / 1Mb}} | foreach {
    if ( $_.name -ne $pathToFile )
    {
    if(!(Test-Path -Path $containerPath))
    {
    Copy-Item $currentPath -Destination $containerPath -recurse
    }
    if(Test-Path -Path $containerPath)
    {
    $value = $_.Fullname + ',' + $_.Mbytes
    Add-Content c:\temp\Content-Cleanup.csv  -value $value
    Remove-Item $_.FullName
    }
    }
    }
    }
    }
    }
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer