Advertisement
dropbox1349

ForEach-Object cmdlet & foreach loops

Dec 16th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $movies = @()
  2. (get-content C:\Path\Test4.txt) | foreach($_){
  3. $properties = @{
  4. date = $_.substring($_.IndexOf("(")+1,4)
  5. name = $_.substring(0,$_.IndexOf("("))
  6. }
  7. write-host $date
  8. write-host $name
  9.  
  10. $movies += New-Object PSObject -Property $properties
  11. }
  12.  
  13. $torrentFiles = dir $torrentPath
  14.  
  15. foreach($movie in $movies){
  16. $datePath = "C:\Path\$($movie.date)"
  17. if(-not(test-path $datePath)) {
  18. new-item $datePath -ItemType "directory"
  19. }
  20. $words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
  21. $significant = $words.Count
  22.  foreach($torrentFile in $torrentFiles){
  23.  $matchingWords = 0
  24.   foreach($word in $words){
  25.    if($torrentFile.BaseName -match $word){
  26.     $matchingWords += 1
  27.    }
  28.   }
  29.   if($matchingWords -ge $significant){
  30.   Move-Item -path $torrentfile -Destination $datePath
  31.  }
  32.  }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement