How to match array elements with a file path

Welcome Forums General PowerShell Q&A How to match array elements with a file path

Viewing 2 reply threads
  • Author
    Posts
    • #202952
      Participant
      Topics: 11
      Replies: 4
      Points: 52
      Rank: Member

      I am trying to list out the files except any directory which contains the array elements.
      i.e The file paths which matches any values of the array should be skipped.

      The below is my code. But its returning file paths including the items in the array. It should not.

      Could anyone suggest how to skip the files which contain array strings anywhere in the path.

      $skipitems 	= @('params','workers')
      $srcpath 	= 'D:\dinesh\myproject'
      
      
      $parent_items  = Get-ChildItem -Path $srcpath -Recurse | Where-Object {!$_.PSIsContainer -and $_.Extension -ne '.exe'}
      ForEach($parent_item in $parent_items){
      	
      	#If($skipitems -notmatch parent_item.FullName){
      	If($skipitems | Where-Object{(split-path $parent_item.FullName -parent) -notmatch $_}){
      		
      		Write-Host $parent_item.Fullname
      		
      	}	
      }
      

      Thanks
      Dinesh

      • This topic was modified 1 month, 2 weeks ago by kvprasoon. Reason: code formatting
    • #202958
      Participant
      Topics: 12
      Replies: 1491
      Points: 1,997
      Helping Hand
      Rank: Community Hero

      Match is a Regex operator, so you can’t do arrays directly, but you can do an OR and if you need to use an array you can use -join, something like this:

      PS C:\Users\rasim> Get-ChildItem -Path C:\Scripts -Directory
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Corp                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Legal                                                                                                                                                                                                           
      d-----        12/6/2019   9:04 AM                Sales                                                                                                                                                                                                           
      
      
      
      PS C:\Users\rasim> Get-ChildItem -Path C:\Scripts -Directory | Where{$_.FullName -notmatch 'Corp|Sales'}
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Legal     
      
      
      PS C:\Users\rasim> 'Corp','Sales' -join '|'
      Corp|Sales
      
    • #202964
      Participant
      Topics: 11
      Replies: 4
      Points: 52
      Rank: Member

      Thank You so much! It works perfectly.

Viewing 2 reply threads
  • The topic ‘How to match array elements with a file path’ is closed to new replies.