title: Recursively search for each item in txt file in multiple folders
body: <p>I want to be able to do optimized search for each subfolder that not matches my files in my txt file.</p>
  
  <p>Example of txt file:</p>
  
  <pre><code>1.2.0.0_009           
  1.2.0.0_008           
  1.2.0.0_007           
  1.2.0.0_006           
  1.2.0.0_005           
  1.2.0.0_004           
  1.2.0.0_003           
  1.2.0.0_002           
  1.2.0.0_001           
  1.1.0.0_106           
  1.1.0.0_105           
  1.1.0.0_104           
  1.1.0.0_103           
  1.1.0.0_102           
  1.1.0.0_101           
  1.1.0.0_100           
  1.0.0.0_089           
  1.1.0.0_099                    
  1.0.0.0_088           
  1.0.0.0_087           
  1.0.0.0_086           
  1.0.0.0_085           
  1.0.0.0_084           
  1.0.0.0_083           
  1.0.0.0_082           
  1.0.0.0_081           
  1.0.0.0_080           
  1.0.0.0_079           
  1.0.0.0_078           
  1.0.0.0_077           
  1.0.0.0_076           
  1.0.0.0_075           
  1.0.0.0_074           
  1.0.0.0_073           
  1.0.0.0_072           
  1.0.0.0_071           
  1.0.0.0_070           
  1.0.0.0_069           
  1.0.0.0_068           
  1.0.0.0_067           
  1.0.0.0_066           
  1.0.0.0_065           
  1.0.0.0_064           
  1.0.0.0_063           
  1.0.0.0_062           
  1.0.0.0_061           
  1.0.0.0_060                    
  1.0.0.0_052           
  1.0.0.0_051           
  1.0.0.0_050           
  1.0.0.0_049           
  1.0.0.0_048           
  1.0.0.0_047           
  1.0.0.0_046           
  1.0.0.0_045           
  1.0.0.0_044           
  1.0.0.0_043           
  1.0.0.0_042           
  1.0.0.0_041           
  1.0.0.0_040           
  1.0.0.0_039           
  1.0.0.0_038           
  1.0.0.0_037           
  1.0.0.0_036_1         
  1.0.0.0_036              
  0.0.0.1_89            
  0.0.0.1_88            
  0.0.0.1_87            
  0.0.0.1_86            
  0.0.0.1_85_1          
  0.0.0.1_85            
  0.0.0.1_84            
  0.0.0.1_83            
  0.0.0.1_82            
  0.0.0.1_81            
  0.0.0.1_80_1          
  0.0.0.1_80            
  0.0.0.1_79_1          
  0.0.0.1_79            
  0.0.0.1_78            
  0.0.0.1_77            
  0.0.0.1_76            
  0.0.0.1_75            
  0.0.0.1_74            
  0.0.0.1_73            
  0.0.0.1_72            
  0.0.0.1_71            
  0.0.0.1_70            
  0.0.0.1_69            
  0.0.0.1_68            
  0.0.0.1_67            
  0.0.0.1_66            
  0.0.0.1_65            
  0.0.0.1_64            
  0.0.0.1_63            
  0.0.0.1_62            
  0.0.0.1_61            
  0.0.0.1_60            
  0.0.0.1_59            
  0.0.0.1_58            
  0.0.0.1_57            
  0.0.0.1_56            
  0.0.0.1_55            
  0.0.0.1_54            
  0.0.0.1_53            
  0.0.0.1_52_1          
  0.0.0.1_52            
  0.0.0.1_51            
  0.0.0.1_50            
  0.0.0.1_49            
  0.0.0.1_48            
  0.0.0.1_47_1          
  0.0.0.1_47            
  0.0.0.1_46            
  0.0.0.1_45            
  0.0.0.1_44_1          
  0.0.0.1_44            
  0.0.0.1_43_1          
  0.0.0.1_43            
  0.0.0.1_42            
  0.0.0.1_41            
  0.0.0.1_40            
  0.0.0.1_39            
  0.0.0.1_38            
  0.0.0.1_37            
  0.0.0.1_36            
  0.0.0.1_35            
  0.0.0.1_34            
  0.0.0.1_33            
  0.0.0.1_32            
  0.0.0.1_31_1          
  0.0.0.1_31            
  0.0.0.1_30            
  0.0.0.1_29            
  0.0.0.1_28            
  0.0.0.1_27            
  0.0.0.1_26            
  0.0.0.1_25            
  0.0.0.1_24            
  0.0.0.1_23            
  0.0.0.1_22            
  0.0.0.1_21            
  0.0.0.1_20            
  0.0.0.1_19            
  0.0.0.1_18            
  0.0.0.1_17            
  0.0.0.1_168           
  0.0.0.1_167           
  0.0.0.1_166           
  0.0.0.1_165           
  0.0.0.1_164           
  0.0.0.1_163           
  0.0.0.1_162           
  0.0.0.1_161   
  </code></pre>
  
  <p>Now the first digits represnts folder (for e.g. 1.2.0.0 is a folder) which contains files (009,008, etc...). But also that folder contains <strong>and other files that are not contained in my txt file</strong>. I want to compare this txt file with each folder in my MainFolder:</p>
  
  <pre><code>Mode                LastWriteTime         Length Name
  ----                -------------         ------ ----
  d-----       11/22/2018   1:04 PM                0.0.0.1
  d-----       11/22/2018   1:04 PM                1.0.0.0
  d-----       11/22/2018   1:04 PM                1.1.0.0
  d-----       11/22/2018   1:04 PM                1.2.0.0
  -a----       11/13/2018  11:54 AM          70656 DbUp.dll
  -a----       11/13/2018  11:54 AM         230912 DbUp.pdb
  </code></pre>
  
  <p>and copy only those which are not contained in txt file.</p>
  
  <p>My Approach:</p>
  
  <pre><code>[string]$SourceTxtDbFile = "C:\file.txt"
  $delimitedFile = Get-Content $SourceTxtDbFile | ConvertFrom-String -Delimiter "_" -PropertyNames DbVersion, ScriptNumber
  
  #foreach file in $delimitedFile
  foreach ($file in $file_list){
  # foreach file in the folders
  foreach($dir in (Get-ChildItem $source -Recurse)){
      # if the file name is in diretocry listed
      if($file -eq $dir.name){
          # copy only once, if the document name already exists, skip
          if(-not(test-path "$destination\$file")){
              # copy the file
              Copy-Item $dir.fullname -Destination $destination -Verbose
          }
       }
     }
  }
  </code></pre>
  
  <p>But I assume that it can be more optimized code.  </p>
  
  <p>UPDATE:
  By the courtesy of @LotPings
  Here is the working code (edited by myself):</p>
  
  <pre><code> $uatScripts = (Get-Content $uatFile).Trim() # there were trailing spaces
  
  Push-Location $source
  
  Get-ChildItem *\* -File | ForEach-Object {
      $LookUp = "{0}" -f $_.BaseName
      If ($uatScripts -notcontains $LookUp) {
          Copy-Item $_.FullName -Destination $destination
      }
  }
  </code></pre>