Jacky Ned
Jacky Ned

Reputation: 23

Make folder structure for files spitting strings in file txt using foreach loops

I have these files

Irrational Man.pdf
Il Ponte Delle Spie (2015).pdf
Interiors.pdf
Le Ragazze della Terra Sono Facili.pdf
Star Wars Il Risveglio Della Forza.pdf

My script (look below) make this situation: script that I use

but I should to order files also by genre inside each year folder like so: script that I expect

Strings inside Text4.txt is so formatted

Irrational Man (2015) #Commedy#
Il Ponte Delle Spie (2015) #Thriller#
Interiors (1978) #dramatic#
Le Ragazze della Terra Sono Facili (1989) #adventure, comedy#
Star Wars Il Risveglio Della Forza (2015) #science fiction, adventure, comedy#

For each files moved inside year folders like

1978
1989
2015

I need to order each files inside by genre also that I find in Text4.txt

This is the folder structure that I expected (files are only with .pdf extensions)

1978
  |__dramatic
  |      |_Interiors.pdf

1989
  |__adventure, comedy
  |    |_Le Ragazze della Terra Sono Facili.pdf

2015
  |__comedy
  |    |_Irrational Man.pdf
  |
  |__ science fiction, adventure, comedy
  |                |_Star Wars Il Risveglio Della Forza.pdf
  |
  |__ thriller
         |_Il Ponte Delle Spie (2015).pdf
         |_xxxx.pdf

This is code that I use

$movies = @()
(get-content C:\Path\Test4.txt) | foreach($_){
$properties = @{
date = $_.substring($_.IndexOf("(")+1,4)
name = $_.substring(0,$_.IndexOf("("))
}
write-host $date
write-host $name

$movies += New-Object PSObject -Property $properties
}

$torrentFiles = dir $torrentPath

foreach($movie in $movies){
$datePath = "C:\Path\$($movie.date)"
if(-not(test-path $datePath)) {
new-item $datePath -ItemType "directory"
}
$words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
$significant = $words.Count
 foreach($torrentFile in $torrentFiles){
 $matchingWords = 0
  foreach($word in $words){
   if($torrentFile.BaseName -match $word){
    $matchingWords += 1
   }
  }
  if($matchingWords -ge $significant){
  Move-Item -path $torrentfile -Destination $datePath
 }
 }
}

I try to add genre variable with regex to delimitate position of string to make the correctly folders

$properties = @{
date = $_.substring($_.IndexOf("(")+1,4)
name = $_.substring(0,$_.IndexOf("("))
genre = $_.substring($_.IndexOf("#")$"#")
}
write-host $date
write-host $name
write-host $genre

But I can't understand how implement genre variable inside this piece of code

$words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
$significant = $words.Count
 foreach($torrentFile in $torrentFiles){
 $matchingWords = 0
  foreach($word in $words){
   if($torrentFile.BaseName -match $word){
    $matchingWords += 1

Upvotes: 0

Views: 116

Answers (2)

user6811411
user6811411

Reputation:

Edit completed the script, but can't test the $torrentfiles/part Especially the word count.
Please change the vars $File $Base and $torrentpath to fit your needs.

#SortTo-Folders.ps1
$File = ".\Text4.txt"
$Base = "C:\Path\"
$torrentPath = "."
$movies = @()

## Evaluate RegEx https://www.regex101.com/r/F24BfH/1
$RegEx = '^(?<Name>[^\(]+)\(.*?(?<Date>\d+)\).#(?<Genre>[^#]+)#.*$'
(get-content $File ) | foreach{
    If ($_ -match $RegEx){
        $properties = @{
            date = $matches.Date
            name = $matches.Name
            genre = $matches.Genre 
        }
        $movies += New-Object PSObject -Property $properties
    }
}
$movies
#--------------------------------------------------------------

$torrentFiles = Dir $torrentPath

foreach($movie in $movies){
    $DateGenrePath = "$Base$($movie.date)\$($movie.genre)"
    if(!(test-path $DateGenrePath)) {
        new-item $DateGenrePath -ItemType "directory"
    }
    $words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
    $significant = $words.Count
    foreach($torrentFile in $torrentFiles){
        $matchingWords = 0
        foreach($word in $words){
            if($torrentFile.BaseName -match $word){
                $matchingWords += 1
            }
        }
        if($matchingWords -ge $significant){
            Move-Item -Path $torrentFile -Destination $DateGenrePath -whatif
        }
    }
}

To see the RegEx working, visit https://www.regex101.com/r/F24BfH/1

Upvotes: 1

Esperento57
Esperento57

Reputation: 17462

my solution

$dirwithfile="c:\temp\test"
$Newdirfile="C:\temp\test2"

#template for describe your datas
$template=@"
{Title*:Word1 word2} ({Year:1234}) #{Categories:Category1}#
{Title*:Word3} ({Year:4567}) #{Categories:Category2, Category1, Category3}#
{Title*:Word5 Word6} ({Year:1956}) #{Categories:Category1 Category2, Category3, Category4}#
"@

#cut datas and build list with all necessery data
$listdata=gc C:\temp\text4.txt | ConvertFrom-String -TemplateContent $template | %{ 
[pscustomobject]@{
                    Title=$_.Title;
                    Year=$_.Year;
                    Categorie=$_.Categories; 
                    PathBefore="$dirwithfile\$($_.Title).pdf";
                    PathAfter="$Newdirfile\$($_.Year)\$($_.Categories)"
                 }
}

$listdata | %{New-Item -ItemType Directory $_.PathAfter -Force;  Move-Item $_.PathBefore $_.PathAfter -Force }

Upvotes: 1

Related Questions