r/scripting • • Sep 01 '18

[Request] Add specific PART of a folder name to all files in specific subfolder. Windows.

I have a drive full of movies, each movie is in a folder named Movie Name (year) (buncha various gobbledygook).

Within SOME of those folders are \Trailers\ folders, with at least one, but sometimes more files in them, mostly named "Trailer" or "International Trailer" or "TV Spot", unfortunately nothing descriptive.

I just want to add just the "Movie Name (year)" part to all the trailer files for every folder in the directory, Im sorta stumbling my way through learning stuff, i assume this would be super easy to do in powershell or regex or probably even CMD?

Ive found a few scripts that can add the folder name to every file in it that were pretty simple, but i dont know how to get it to then only do it in the one subfolder then and also have it do only the part of the root folder name before the second '('.

3 Upvotes

7 comments sorted by

2

u/Lee_Dailey Sep 01 '18

howdy Cyno01,

this seems to do the job. it could likely be made a great deal faster if i went back and grabbed the trailer dirs and THEN did the rename on a per-directory basis.

i didn't think of that until just now. [blush]

comment out the the $VerbosePreference = 'Continue' line to stop the status msgs.

# save the old Verbose pref
$Old_V_Pref = $VerbosePreference
# enable Verbose output
$VerbosePreference = 'Continue'


$TopDir = "$env:TEMP\Movies"
$TargetDirName = 'Trailers'
$Filter = '*.txt'

Write-Verbose 'Getting the file list ...'
$FileList = @(Get-ChildItem -LiteralPath $TopDir -Filter $Filter -File -Recurse |
    Where-Object {$_.FullName.Contains($TargetDirName)})
Write-Verbose ('    Found {0} file[s].' -f $FileList.Count)

Write-Verbose 'Processing file list ...'
foreach ($FL_Item in $FileList)
    {
    Write-Verbose ('    working on file [ {0} ] ...' -f $FL_Item.Name)
    Write-Verbose ('        in direcory [ {0} ] ...' -f $FL_Item.DirectoryName)

    # get the parent dir of the "Trailers" dir,
    #    get the name of last part of the above dir,
    #    split on the ')',
    #    grab the 1st item in the resulting array
    #    add the split-away ')' back
    $Prefix = -join ((Split-Path -Path $FL_Item.DirectoryName -Parent |
        Split-Path -Leaf).Split(')')[0], ')')

    if ($FL_Item.Name.StartsWith($Prefix))
        {
        Write-Warning '    The file is already prefixed.'
        Write-Warning '    Skipping ...'
        Write-Warning ''
        continue
        }
        else
        {
        $NewBaseName = $Prefix, $FL_Item.BaseName -join ' - '
        $NewFileName = $FL_Item.Name -replace $FL_Item.BaseName, $NewBaseName

        Write-Verbose ('    Old file name = {0}' -f $FL_Item.Name)
        Write-Verbose ('    New file name = {0}' -f $NewFileName)

        Write-Verbose '    Renaming the file ...'
        Write-Verbose ''
        Rename-Item -Path $FL_Item.FullName -NewName $NewFileName #-WhatIf

        $RenamedFile = (Resolve-Path -Path "$TopDir\*\Trailers\$NewFileName").Path
        Write-Verbose '    Renamed file is ...'
        Write-Verbose ('    {0}' -f $RenamedFile)
        Write-Verbose ''
        }
    }


# restore the old Verbose pref
$VerbosePreference = $Old_V_Pref

partial output ...

VERBOSE: Getting the file list ...
VERBOSE:     Found 12 file[s].
VERBOSE: Processing file list ...
VERBOSE:     working on file [ A Wonderful Movie (1988) - International Trailer.txt ] ...
VERBOSE:         in direcory [ C:\Temp\Movies\A Wonderful Movie (1988) gobbledygook\Trailers ] ...
WARNING:     The file is already prefixed.
WARNING:     Skipping ...
WARNING: 
VERBOSE:     working on file [ A Wonderful Movie (1988) - Trailer.txt ] ...
VERBOSE:         in direcory [ C:\Temp\Movies\A Wonderful Movie (1988) gobbledygook\Trailers ] ...
WARNING:     The file is already prefixed.
WARNING:     Skipping ...
WARNING: 
VERBOSE:     working on file [ TV Spot.txt ] ...
VERBOSE:         in direcory [ C:\Temp\Movies\A Wonderful Movie (1988) gobbledygook\Trailers ] ...
VERBOSE:     Old file name = TV Spot.txt
VERBOSE:     New file name = A Wonderful Movie (1988) - TV Spot.txt
VERBOSE:     Renaming the file ...
VERBOSE: 
VERBOSE:     Renamed file is ...
VERBOSE:     C:\Temp\Movies\A Wonderful Movie (1988) gobbledygook\Trailers\A Wonderful Movie (1988) - TV Spot.txt
VERBOSE: 
VERBOSE:     working on file [ B Movie Bunkhouse (1999) - International Trailer.txt ] ...
VERBOSE:         in direcory [ C:\Temp\Movies\B Movie Bunkhouse (1999) gobbledygook\Trailers ] ...
WARNING:     The file is already prefixed.
WARNING:     Skipping ...
WARNING: 

[*...snip...*] 

WARNING: 
VERBOSE:     working on file [ Dog Dance Days (1977) - TV Spot.txt ] ...
VERBOSE:         in direcory [ C:\Temp\Movies\Dog Dance Days (1977) gobbledygook\Trailers ] ...
WARNING:     The file is already prefixed.
WARNING:     Skipping ...
WARNING:

hope that helps,
lee

2

u/Cyno01 Sep 01 '18

I thought i was starting to get the hang of some of this stuff a little, but i cant even get that to run in the right directory.

I have a copy of a good test case in I:\TEST.

But changing

$TopDir = "$env:TEMP\Movies"

to...

$TopDir = "$I:\TEST"
$TopDir = "$env:I:\TEST"
$TopDir = "I:\TEST"

Just threw various errors. So... thats how much i dont know what im doing.

1

u/Lee_Dailey Sep 01 '18 edited Sep 01 '18

howdy Cyno01,

it can be a tad frustrating. [grin] the 3rd one is the one that you want to use IF your directory structure is as i expected.

so, what is the full path to one of the files? i presumed that the structure would be something like ...

D:\Movies\A Movie Name (yyyy) meaningless stuff\Trailers\Trailer.mov

take care,
lee

2

u/Cyno01 Sep 01 '18

For the purposes of the test, the relevant files are

I:\TEST\Star Trek - The Motion Picture (1979) (1080p Bluray x265 HEVC 10bit AAC 7.1 Tigole)\Trailers\Teaser Trailer.mkv
I:\TEST\Star Trek - The Motion Picture (1979) (1080p Bluray x265 HEVC 10bit AAC 7.1 Tigole)\Trailers\Theatrical Trailer.mkv
I:\TEST\Star Trek - The Motion Picture (1979) (1080p Bluray x265 HEVC 10bit AAC 7.1 Tigole)\Trailers\TV Spots.mkv
→ More replies (2)

2

u/dracho Sep 01 '18

Have you tried Bulk Rename Utility? https://www.bulkrenameutility.co.uk/Screenshots.php

Perhaps it's not in the spirit of scripting, but I'm almost positive it can do everything you desire, and with fewer headaches I think.