从文件文本中的字符串行中提取年份,并将torrent移动到按年

时间:2016-09-12 11:39:12

标签: powershell batch-processing

问题很棘手,因为这是我先前问题的演变。

要在文件夹中移动torrent我使用此PowerShell脚本

$ToFolder = "$env:USERPROFILE\Desktop\to"
$FromFolder = "$env:USERPROFILE\Desktop\From"

#Create the sample folder on your desktop
#This line can be commented out if your ToFolder exists
New-Item $ToFolder -ItemType directory -Force

GCI -Path $FromFolder *.torrent | % {
    if ($_.Name -match "(19|20)\d{2}") {

        #Check to see if year folder already exists at the destination
        #If not then create a folder based on this year
        if (!(Test-Path "$ToFolder\$($Matches[0])")) {
            New-Item -Path "$ToFolder\$($Matches[0])" -ItemType directory
        }

        #Transfer the matching file to its new folder
        #Can be changed to Move-Item if happy with the results
        Move-Item -Path $_.FullName -Destination "$ToFolder\$($Matches[0])" -Force
    }
}

但在我的新情况下,我必须从文件 .txt

中提取年份

文件夹

中文件 .torrent 的示例列表
Caccia Spietata.torrent
Caccia Zero terrore del Pacifico.torrent
Caccia.A.Ottobre.Rosso.torrent
Cacciatore Bianco Cuore Nero.torrent
Cacciatore di Ex.torrent
Cacciatori Di Zombie.torrent

文件文本中的字符串列表示例

Caccia grossa a casa di Topolino (2006)
Caccia selvaggia [HD] (1981)
Caccia spietata (2006)
Cacciatori Di Zombie (2005)

什么脚本必须做?

A。从文件文本中的字符串中提取年份(每个字符串都在一行上,因为文件文本是列表
N.B 脚本应该比较torrent文件名和文件列表中的字符串。

Caccia spietata (2006)

提取年份仅适用于平等文本或非常相似的文本,如

Caccia Spietata.torrent
Caccia spietata (2006)

如果我有

caccia.spietata.torrent
caccia SPiETata (2006)

这对我来说非常相似。

B。制作文件夹

2006

C。移动种子

Caccia Spietata.torrent

进入文件夹2006

我想要这个解决方案,因为我有很多没有年份的.torrent文件名,所以我必须按年重新排序。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

第一个障碍是解析字符串文件中的日期和名称。然后,将它们添加到电影名称字符串的散列中。

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

将电影名称和日期分开后,您可以遍历每部电影并创建一个文件夹(如果该文件夹不存在)。

foreach($movie in $movies){
$movie.date
$datePath = "C:\Path\$($movie.date)"
if(-not(test-path $datePath)) {
new-item $datePath -ItemType "directory"
}

之后,您可以根据空格将名称拆分为关键词。

$words = $movie.name -split '\s'
$words

以下是我休息时的情况。下一步看起来有点复杂,因为您必须根据关键字将torrent文件与散列中的对象进行匹配。如果不访问原始数据,很难构建这样的过滤器。我的第一个想法是基于fileName.torrent -like "*word*"匹配,但看起来有很多重复的单词。下一个选项是匹配多个单词,或者可能只使用不常见的单词(不包括“caccia”,文章等)。无论哪种方式,这应该让你更接近你的目标。也许其他人可以帮忙完成,或者我可以在另一个休息期间再次访问它。

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

foreach($movie in $movies){
$movie.date
$datePath = "C:\Path\$($movie.date)"
if(-not(test-path $datePath)) {
new-item $datePath -ItemType "directory"
}
$words = $movie.name -split '\s'
$words
#this is as far as I got
}

<强>更新

我添加了一些我们在评论中谈到过的内容。大多数更改都在脚本的底部。

$movies = @()
(get-content $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
}
#no significant changes were made above this point
$torrentFiles = dir $torrentPath

foreach($movie in $movies){
$datePath = "$Path\$($movie.date)"
if(-not(test-path $datePath)) {
new-item $datePath -ItemType "directory"
}
$words = ($movie.name -split '\s') | ?{ $_.Length -gt 1}
#this is as far as I got last time; most of the changes are below, though I did change 
#just a bit above

#this sets a number of words which needs to match. Currently, it has to match 
#on all words. If you wanted, you set it to a static number (2)
#     or do something like $words.count -1. There is a commented-out example of 
#such a solution.
$significant = $words.Count
#if($words.Count -eq 1){$significant = 1}
#else{$significant = ($words.Count - 1)

# here you loop through the torrentfiles, finding files whose base names have a 
#significant number of matching words with the string
 foreach($torrentFile in $torrentFiles){
 $matchingWords = 0
  foreach($word in $words){
   if($torrentFile.BaseName -match $word){
    $matchingWords += 1
   }
  }
  if($matchingWords -ge $significant){
  $_ | Move-Item -Destination $datePath
  }
 }
}