我有这些文件
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
Text4.txt中的字符串格式化
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#
对于在年份文件夹中移动的每个文件,例如
1978
1989
2015
我还需要按照我在Text4.txt中找到的 中的每个文件来订购
这是我期望的文件夹结构(文件只有.pdf扩展名)
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
这是我使用的代码
$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
}
}
}
我尝试使用正则表达式添加genre变量来分隔字符串的位置以制作正确的文件夹
$properties = @{
date = $_.substring($_.IndexOf("(")+1,4)
name = $_.substring(0,$_.IndexOf("("))
genre = $_.substring($_.IndexOf("#")$"#")
}
write-host $date
write-host $name
write-host $genre
但是我无法理解在这段代码中如何实现genre变量
$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
答案 0 :(得分:1)
编辑完成了脚本,但无法测试$ torrentfiles / part特别是字数。
请更改vars $ File $ Base和$ torrentpath以满足您的需求。
#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
}
}
}
要查看RegEx的工作情况,请访问https://www.regex101.com/r/F24BfH/1
答案 1 :(得分:1)
我的解决方案
$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 }