I have archives with filenames like this
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
In same folder I have a text.txt file formatted in this way
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
I try to do this:
1 . create folders using the first fields from the text file
2 . move the files of the same families to their respective folders
For example, to extract the year from each line of the txt text file and create the related folders I use this code
$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
}
For example if I want to implement a Regex in powershell script to generate folders and move files in relative folders I can use this
I use a similar script to generate and move files into folders.
$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
}
}
I'm trying to fit and chain the 2 codes but for a different purpose (as shown in the image, not to extract year), but I can't figure out how to do it.
Your input file text.txt
can be treated as a semi-colon ( ;
) delimited CSV file, so we can use the Import-Csv
cmdlet on it to make things a lot easier:
$sourcePath = 'D:\Test'
$inputFile = Join-Path -Path $sourcePath -ChildPath 'text.txt'
# because the title may contain invalid characters for a folder name like ':'
# create a regex to remove those if applicable
$invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
Import-Csv -Path $inputFile -Delimiter ';' -Header Title,FileName,Link | Group-Object Title | ForEach-Object {
# combine the Title with the source path. Remove invalid characters from the Title
$targetFolder = Join-Path -Path $sourcePath -ChildPath ($_.Name -replace $invalid)
# if the destination folder does not already exist, create it
if (!(Test-Path -Path $targetFolder -PathType Container)) {
$null = New-Item -Path $targetFolder -ItemType Directory
}
foreach ($fileName in $_.Group.FileName) {
$targetFile = Join-Path -Path $sourcePath -ChildPath $fileName
if (Test-Path -Path $targetFile -PathType Leaf) {
Move-Item -Path $targetFile -Destination $targetFolder -Force
}
else {
Write-Warning "File '$targetFile' not found!"
}
}
}
Before:
D:\TEST
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
After:
D:\TEST
| text.txt
|
+---Citadel Forged with Fire
| first_name.part1.rar
| first_name.part1.rev
| first_name.part2.rar
| first_name.part2.rev
|
\---Eurotrack Simulator
second_name.part01.rar
second_name.part01.rev
second_name.part02.rar
second_name.part02.rev
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.