Search code examples
powershelldirectoryrename-item-cmdlet

Rename Proposition to end of directory title


I have to change the name of approximately 600 directories to conform with the the business naming standards and it is proving to be quite complicated.

I will present the problem in a music directory format because it will be easier to explain and relate to.

If I wanted to rename all my Music Directories that have it will display prepositions at the end and not the start

Original

The Beatles

Desired Output

Beatles ,The

I have the following directories as examples

Mode                LastWriteTime     Length Name                              
----                -------------     ------ ----                              
d----        28/08/2016   3:51 PM            Alabama Shakes                    
d----        28/08/2016   3:51 PM            Cat Stevens                       
d----        28/08/2016   3:41 PM            The Beatles                       
d----        28/08/2016   3:42 PM            The Brian Jonestown Massacre      
d----        28/08/2016   3:42 PM            The Rolling Stones                
d----        28/08/2016   3:43 PM            Them                              
d----        28/08/2016   3:43 PM            Them Rotten Vultures              
d----        28/08/2016   3:42 PM            Van Morrison

I have managed to write something to filter the results down to just the ones I want renamed

Get-ChildItem -Filter "THE *"

However I don't know how to puss the logic to rename them all. I'm sure it's going to be recursive and it will use the Rename-Item function.

However I tried the following

Get-ChildItem -Filter "The *" -Recurse | 
    Rename-Item -NewName { $_.name -replace 'The ','' } | 
    Rename-Item -NewName { $_.name + " ,The" }

and got the following results

Mode                LastWriteTime     Length Name                              
----                -------------     ------ ----                              
d----        28/08/2016   3:51 PM            Alabama Shakes                    
d----        28/08/2016   3:41 PM            Beatles                           
d----        28/08/2016   3:42 PM            Brian Jonestown Massacre          
d----        28/08/2016   3:51 PM            Cat Stevens                       
d----        28/08/2016   3:42 PM            Rolling Stones                    
d----        28/08/2016   3:43 PM            Them                              
d----        28/08/2016   3:43 PM            Them Rotten Vultures              
d----        28/08/2016   3:42 PM            Van Morrison                      

Any assistance would be kindly appreciated

Related question will the child item also detect the files? As only directories require to renamed


Solution

  • Managed to get it sorted with the following Code It only modifies the directories and not the files contained in the directory.

    Get-ChildItem -Filter "The *" -Directory|
        ?{ $_.PSIsContainer } | 
        Rename-Item -NewName { $_.name + " ,The" }
    
    
    Get-ChildItem -Filter "The *" -Directory|
        ?{ $_.PSIsContainer } | 
        Rename-Item -NewName { $_.name -replace 'The ','' }