从文件文本中的字符串行中提取年份并将 torrent 移动到按年份重新排序的相关文件夹
Extract year from string row in file text and move torrent to relative folder reordered by year
问题很棘手,因为它是我之前问题的演变。
要在文件夹中移动 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. 从文件文本中的字符串中提取年份(每个字符串都在一行中,因为文件文本是一个 list)
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 文件名,所以我必须按年份正确地重新排序它们。
感谢您的帮助。
第一个障碍是从字符串文件中解析日期和名称。然后将它们添加到电影名称字符串的散列中。
$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
}
}
}
问题很棘手,因为它是我之前问题的演变。
要在文件夹中移动 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. 从文件文本中的字符串中提取年份(每个字符串都在一行中,因为文件文本是一个 list)
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 文件名,所以我必须按年份正确地重新排序它们。
感谢您的帮助。
第一个障碍是从字符串文件中解析日期和名称。然后将它们添加到电影名称字符串的散列中。
$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
}
}
}