Fix moving files into year folder if exist a matching in .txt file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Fix moving files into year folder if exist a matching in .txt file



I have files like this


Taxi driver.torrent
Tabu - Gohatto.txt
Troll 2 (1990)..zip



Inside my filelist.txt file I have files named like so


Troll 2 (1990) [BDrip 1080p - H264 - Ita Ac3] Horror, Commedia
Troll 2 (1990) [XviD - Ita Mp3]
Taxi Driver (1976) Mastered..
Tabù - Gohatto (N. Oshima, 1999)



I have just folders like


1976
1990
1999



I want to move files into correct year folder in this way


1976
|__ Taxi driver.torrent

1990
|__ Troll 2 (1990)..zip

1999
|__ Tabu - Gohatto.txt



I use this path, folders


C:Path
Test4.txt
script_powershell.ps1



I actually test with powershell 5 to move


$movies = @()
(get-content C:PathTest4.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')



EDIT:



This poweshell has many problems. For examples


Caccia al delitto



is moved in 1990 folder...but..Caccia al delitto is 1986..
Inside file text I have


Caccia a Ottobre Rosso (1990) [DivX - Ita Mp3] Guerra [CURA] Russia
Caccia a Ottobre Rosso (1990) [VP9 - Ita Eng Opus] Thriller



I have not a text string for Caccia al delitto (I remove it for test)





This is obviously a crossposting of superuser.com/questions/1348240/… but with differing details. You could ease helping by using one identity sharing the same details. See my answer on SU
– LotPings
Aug 12 at 20:00





yes, you are right, I analyze in details your solution on SU and I answer you, look also my comment below on this page please. Problems seems about if you use a long list so spam folders are created and a wrong matching are performed so many files are moved in wrong year folders.. I test now
– user3520363
Aug 12 at 20:10




1 Answer
1


$movies = @()
$movieLocation = 'C:Path'
$torrentPath = '.'

(get-content "$movieLocationTest4.txt") | foreach($_)

# Check for braces.
if (-not($_ -match ".*(.*).*")) return

$properties = @
date = ($_ -replace ".+?(.*?(d4).*?).*", '$1')
name = $_.substring(0, $_.IndexOf("(")).Trim()


# Add items that have a 4 digit date.
if ($properties.date -match "^d4$")
'Name: "' + $properties.name + '" Date: "' + $properties.date + '"'

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



$torrentFiles = dir $torrentPath

foreach ($movie in $movies)
$datePath = "$movieLocation$($movie.date)"

if (-not(test-path "$datePath"))
new-item "$datePath" -ItemType "directory"


foreach ($torrentFile in $torrentFiles)
# Get percentage based on length.
$pc = [int]($movie.name.length / $torrentFile.basename.length * 100)

# Only between 80% and 100% in length.
if ($pc -gt 100) continue
if ($pc -lt 80) continue

if ($torrentFile.basename -match $movie.name)
# Items that match.
'Torrent: 0,-40 Date: 1,-5 Match: 2' -f $torrentFile.basename, $movie.date, $movie.name

if (-not(test-path "$datePath$torrentfile"))
Move-Item -LiteralPath "$torrentPath$torrentfile" -Destination "$datePath"






A modest fix with the date using regex.
The name required a trim to remove trailing space.
I added a test-path before move-item in case the file is not found.


date


name


test-path


move-item



Long time since last used Powershell so could be perhaps improved more.
I tested in path . so hope C:Path works just as well.


.


C:Path





Problem is more complicated. I test with this list pastebin.com/raw/aRP94peb - all films of this list is of 1990. For my pack files, instead, I want to move only films of 1990 present in the filetext list in 1990 folder but.. if you try to test with this pack files drive.google.com/file/d/1Yg-cDIGeLyIhv0ImOzKW7AGTT03EdNdm/… you can see that many files are moved incorrectly also if is not present in the list and also many spam folders are generated.
– user3520363
Aug 12 at 10:03






You can see, for example, that il_bandito_delle undici is not in the filelist but is moved in 1990..or, just another example "Il Signore Degli Anelli - Il Ritorno Del Re" is not of 1990 year ..but is moved inside 1990 folder.. There are many problems if you have a long list but problem seems about how script is written
– user3520363
Aug 12 at 10:04





Thanks for the extra info and files. I added a check for the date being 4 digits. The name test is set at between 80% and 100% to match. Change the check to improve results. Ensure you set torrent path.
– michael_heath
Aug 12 at 14:15





Hi Micheal, I test your script but there are still many files unprocessed, This pack you find unmoved files, for many is correct because there are strings without year but for other many files exists strings with year in text file, take a look please and test drive.google.com/file/d/1mVNNH2fb8jeNX1xrf4U7wt6ffwHbl_2X/…
– user3520363
Aug 16 at 16:09





I have the previous list of about 300 names yet your latest link has about 8300 files. I tested with ps1 and get 1 match in about 135 seconds. I tested with py and get 20 matches in about 1.5 seconds. The py has a junk string replacement of about 30 words to remove dvd5, dvd9 etc. Even with a list of 8300 names, the py may get matches of about 250 which is not much. Manual comparison shows that many files in the list do not exist so it seems not so much of a code issue.
– michael_heath
Aug 17 at 10:20


dvd5


dvd9






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS