Extended dir Pattern

Author Message
TNO

  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
  • Status: offline
Extended dir Pattern Monday, June 06, 2011 7:04 PM (permalink)
0
Here's a snippet I came up with on a larger project that you might find useful:


Function Rec($path){
 if($path[-1] -eq '/' -or $path[-1] -eq '\'){
 $path += '**'
 }
 $rec = $path -split '**', 0, 'simplematch'
 if($rec.length -gt 1){ #recursive
 dir -r $rec[0] | ?{ !($_.PSIsContainer) -and ($_.FullName -like ($rec -join '*')) } | %{ $_.FullName }
 } else { #non-recursive
 dir $rec[0] | ?{ !$_.PSIsContainer } | %{ $_.FullName }
 }
}

Rec("C:\test\**\sub2\*.sml")
#C:\test\sub0\sub3\sub2\set.sml
#C:\test\sub0\sub3\sub2\vector3.sml
#C:\test\sub1\sub2\set.sml
#C:\test\sub1\sub2\vector3.sml

To iterate is human, to recurse divine. -- L. Peter Deutsch
 
#1
    TomRiddle

    • Total Posts : 620
    • Scores: 12
    • Reward points : 0
    • Joined: 2/7/2008
    • Location: Australia
    • Status: offline
    Re:Extended dir Pattern Tuesday, June 07, 2011 12:17 AM (permalink)
    0
    Hi TNO, Does this return the same results with your file structure?
     
    Get-ChildItem "c:\test" -Recurse | where {$_.basename -eq "sub2"} | foreach {Resolve-Path "$($_.fullname)\*.sml"} 
    'Or with Alias'
    LS c:\test -R|?{$_.basename-eq"sub2"}|%{rvpa "$($_.fullname)\*.sml"}

    -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
     
    #2
      TNO

      • Total Posts : 2094
      • Scores: 36
      • Reward points : 0
      • Joined: 12/18/2004
      • Location: Earth
      • Status: offline
      Re:Extended dir Pattern Tuesday, June 07, 2011 2:58 AM (permalink)
      0
      "Resolve-Path"   Something I wish I knew about earlier.
       
      What would something like this look like?
       
      C:\**\sub1\**\sub7\*.sml
       
       
      To iterate is human, to recurse divine. -- L. Peter Deutsch
       
      #3
        TomRiddle

        • Total Posts : 620
        • Scores: 12
        • Reward points : 0
        • Joined: 2/7/2008
        • Location: Australia
        • Status: offline
        Re:Extended dir Pattern Tuesday, June 07, 2011 10:30 AM (permalink)
        0
        Now that's getting really tricky.
        I am assuming ** represents one or more folders.
         
        So a valid path could be
        C:\sub0\sub1\sub2\sub3\sub7\xx.sml
        or either
        C:\sub0\sub2\sub1\sub3\sub7\yy.sml



        -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
         
        #4
          TNO

          • Total Posts : 2094
          • Scores: 36
          • Reward points : 0
          • Joined: 12/18/2004
          • Location: Earth
          • Status: offline
          Re:Extended dir Pattern Tuesday, June 07, 2011 11:39 AM (permalink)
          0
          Correct. I'm hopeful that you can pull something short and elegant out of thin air
          To iterate is human, to recurse divine. -- L. Peter Deutsch
           
          #5
            TomRiddle

            • Total Posts : 620
            • Scores: 12
            • Reward points : 0
            • Joined: 2/7/2008
            • Location: Australia
            • Status: offline
            Re:Extended dir Pattern Tuesday, June 07, 2011 1:17 PM (permalink)
            0
            My original idea was to use a REGEX to check the path but gave up on that idea after coming up with a script that fit the first scenario. REGEX still seems to be the best course of action but its not one of my strengths.  It seems your recursive script is the most elegant for these multiple path patterns.   I will have another play tonight.
            -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
             
            #6
              TNO

              • Total Posts : 2094
              • Scores: 36
              • Reward points : 0
              • Joined: 12/18/2004
              • Location: Earth
              • Status: offline
              Re:Extended dir Pattern Wednesday, June 08, 2011 11:48 PM (permalink)
              0
              Yeah, this got complex way too fast. I think falling back to "-like" is much smarter:

               function recDir([string]$p){ 
               if($p[-1] -eq '/' -or $p[-1] -eq '\'){ $p += '**' } 
               
               if($p.indexOf('**') -gt -1){ 
               dir -r $p.substring(0,$p.indexOf('**')) |  
               ?{ !$_.PSIsContainer -and $_.FullName -like $p} | %{$_.FullName} 
               } else { 
               dir $p | ?{ !$_.PSIsContainer } | %{ $_.FullName } 
               } 
               } 

              <message edited by TNO on Thursday, June 09, 2011 12:43 AM>
              To iterate is human, to recurse divine. -- L. Peter Deutsch
               
              #7
                TomRiddle

                • Total Posts : 620
                • Scores: 12
                • Reward points : 0
                • Joined: 2/7/2008
                • Location: Australia
                • Status: offline
                Re:Extended dir Pattern Tuesday, June 14, 2011 12:41 AM (permalink)
                0
                I have started to study regex and used this as a practical problem. Here is a regex solution, it is not in a function but if you follow through my workings you probably could rebuild the regex for different directory patterns if required.
                 
                 
                # valid file structure
                $a="C:\sub0\sub1\sub2\sub3\sub7\xA12_ -x.sml"
                $b="C:\sub0\sub2\sub1\sub3\sub7\yy.sml"
                # simple file matches
                $a -match ".*?\.sml"
                $a -match "C:\\.*?\.sml"
                # powershell regex is case insensitive by default so a-z matches uppercase too
                $a -match "[a-z]:\\.*?\.sml"

                # match specific directory structure
                $a -match "[a-z]:\\.*?\\sub1\\.*?sub7\\.*?\.sml"
                # use variables
                $dir1="sub1"
                $dir2="sub7"
                $a -match "[a-z]:\\.*?\\($dir1)\\.*?($dir2)\\.*?\.sml"
                # use variable for extension too
                $ext="\.sml"
                $a -match "[a-z]:\\.*?\\($dir1)\\.*?($dir2)\\.*?($ext)"
                # tighten up the directory matching
                $a -match "[a-z]:\\.*?($dir1).*?($dir2)\\.*?($ext)"
                # tighten up the filename matching
                $a -match "^[a-z]:\\.*?($dir1).*?($dir2)\\[a-z0-9-_\s]{0,256}($ext)$"
                 
                # final script
                dir c:\ -r -ea 0| %{if($_.fullname -match "^[a-z]:\\.*?($dir1).*?($dir2)\\[a-z0-9-_\s]{0,256}($ext)$"){$_.fullname}}
                -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
                 
                #8

                  Online Bookmarks Sharing: Share/Bookmark

                  Jump to:

                  Current active users

                  There are 0 members and 1 guests.

                  Icon Legend and Permission

                  • New Messages
                  • No New Messages
                  • Hot Topic w/ New Messages
                  • Hot Topic w/o New Messages
                  • Locked w/ New Messages
                  • Locked w/o New Messages
                  • Read Message
                  • Post New Thread
                  • Reply to message
                  • Post New Poll
                  • Submit Vote
                  • Post reward post
                  • Delete my own posts
                  • Delete my own threads
                  • Rate post

                  2000-2012 ASPPlayground.NET Forum Version 3.9