【发布时间】:2021-02-02 08:01:00
【问题描述】:
我有这样的文件名的档案
first_name.part1.rar
first_name.part1.rev
first_name.part2.rar
first_name.part2.rev
second_name.part01.rar
second_name.part01.rev
second_name.part02.rar
second_name.part02.rev
在同一个文件夹中,我有一个以这种方式格式化的 text.txt 文件
Citadel: Forged with Fire;first_name.part1.rar;link
Citadel: Forged with Fire;first_name.part1.rev;link
Citadel: Forged with Fire;first_name.part2.rar;link
Citadel: Forged with Fire;first_name.part2.rev;link
Eurotrack Simulator;second_name.part01.rar;link
Eurotrack Simulator;second_name.part01.rev;link
Eurotrack Simulator;second_name.part02.rar;link
Eurotrack Simulator;second_name.part02.rev;link
我尝试这样做:
1。使用文本文件中的第一个字段创建文件夹
2。将同族的文件移动到各自的文件夹中
例如,要从 txt 文本文件的每一行中提取年份并创建相关文件夹,我使用此代码
$movies = @()
(get-content C:\Path\text.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
}
例如,如果我想在 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
Copy-Item -Path $_.FullName -Destination "$ToFolder\$($Matches[0])" -Force
}
}
我正在尝试拟合和链接这 2 个代码,但目的不同(如图所示,不是提取年份),但我不知道该怎么做。
【问题讨论】:
标签: powershell