Reputation: 33
I am pretty new to powershell and have now read too many examples and fried my brain with Get-ChildItem examples.
I have a folder structure for my films as follows
Films
├─Film1
| Film1Name.mp4
│ └─Subs
│ └─English.SRT
│
├─Film2
| Film2Name.mp4
│ └─Subs
│ └─English.SRT
|
├─Film3
| Film3Name.mp4
│ └─Subs
│ └─English.SRT
What I am trying to achieve is to rename and move the *.srt file up a folder level and rename to either the filename of the film or the folder name of the film and insert .ENG to the .SRT filename before the extension. Then delete the unused 'Subs' folder, and do this recursively.
Ending up with the following based on the filename
├─Film1
| Film1Name.mp4
│ Film1Name.ENG.SRT
or this based on the folder name
├─Film1
| Film1.mp4
│ Film1.ENG.SRT
Any help would be most appreciated, either method would work
I was playing with this code from someone elses post and wiped out my test folder
$rootPath = "c:\Test"
Get-ChildItem -Path $rootPath -Directory | ForEach-Object {
$targetFolder = $_.FullName
Resolve-Path "$targetFolder\*" | ForEach-Object {
Move-Item -Path "$_\*.SRT" -Destination $targetFolder -Force
Remove-Item -Path $_
}
}
Upvotes: 3
Views: 208
Reputation: 3246
First you want to get a list of all the top level directories which are named after your "films" after setting the root directory.
Set-Location -Path "Your directory path here"
$Films = Get-ChildItem -Directory
Once we've done that we an iterate through each one, push the location to that directory and check for the existence of a folder called "Subs". Pop-Location
returns us back to the previous location after doing what we need to do.
foreach ($Film in $Films) {
Push-Location $Film
if ((Get-ChildItem -Directory).Name -contains "Subs") {
# Do stuff
}
Pop-Location
}
If we find a "Subs" directory, we want to check for the existence of an ".SRT" files. We may have subtitles for multiple languages.
$Subs = Get-ChildItem -Path *.srt -Recurse
Check if $Subs
is not null.
if ($null -ne $Subs) { }
Move any ".SRT" files to the current location and rename them as specified.
foreach ($Sub in $Subs) {
$FilmPath = $Film.Fullname
$FilmName = $Film.Name
$SubtitleLang = $Sub.BaseName.Substring(0,3).ToUpper()
$Extension = $Sub.Extension
Move-Item -Path $Sub -Destination "$FilmPath\$FilmName.$SubtitleLang$Extension"
}
If you're absolutely sure that you want to remove the "Subs" folder, we will check it's empty first. We can write a message if there are still files remaining instead.
$RemainingFiles = Get-ChildItem -Path Subs -Recurse
if ($null -eq $RemainingFiles) {
Remove-Item -Path Subs -Force
}
else {
Write-Warning "There are still files in $($Film.FullName)\Subs"
}
Putting it all together
Set-Location -Path "Your directory path here"
$Films = Get-ChildItem -Directory
# Loop through the Films
foreach ($Film in $Films) {
Push-Location $Film
if ((Get-ChildItem -Directory).Name -contains "Subs") {
# Find any SRT files in the "Subs" directory
$Subs = Get-ChildItem -Path Subs/*.srt -Recurse
if ($null -ne $Subs) {
foreach ($Sub in $Subs) {
$FilmPath = $Film.Fullname
$FilmName = $Film.Name
$SubtitleLang = $Sub.BaseName.Substring(0,3).ToUpper()
$Extension = $Sub.Extension
Move-Item -Path $Sub -Destination "$FilmPath\$FilmName.$SubtitleLang$Extension"
}
}
}
# Remove the Subs folder
$RemainingFiles = Get-ChildItem -Path Subs -Recurse
if ($null -eq $RemainingFiles) {
Remove-Item -Path Subs -Force
}
else {
Write-Warning "There are still files in $($Film.FullName)\Subs"
}
# Return to the root directory for the next folder.
Pop-Location
}
Upvotes: 2
Reputation: 212
as far as I understood, you know how many folders (Films) you have then this might work:
1..3|ForEach {Copy-Item -Path C:\Films\Film$_\Subs\English.srt -Destination C:\Films\Film$_\Film$_.ENG.SRT}
1..3|ForEach {Remove-Item -Path C:\Films\Film$_\Subs -Recurse -Force}
Upvotes: 0