Rename-Item for not empty folders

Dear all,

I’m just starting at this but I’m stuck in a very simple thing and “Google is not answering me”. I don’t have any knowlage about PowerShell, but sometimes I give a try on simple things to help me on daily basis problems. This week I was trying to upload around 23000 pics to Google Photos and… well, they don’t have any Folder organization system! So (maybe this was not a good decision) I decided to change my folder structure to make it easier for me to organize with albums later.

I’ll try to explain what I want. Any idea, even to make it easier without any code, is welcome:

My folders are like this (1):
\Photos
-2010
–Home
–Beach
–Rio de Janeiro

-2011
–Home
–Sao Paulo
–Rio de Janeiro
—Hotel
—Beach
–Panama

What I tried to do was a simple “Rename-Item” to add, at least, the year at the beginning of every folder. Like this (2):
\Photos
-2010
–2010-Home
–2010-Beach
–2010-Rio de Janeiro

-2011
–2011-Home
–2011-Sao Paulo
–2011-Rio de Janeiro
—2011-Hotel
—2011-Beach
–2011-Panama

Or, if possible, maybe combining the whole path in the name of the last path, like this (3):
\Photos
-2010
–2010-Home
–2010-Beach
–2010-Rio de Janeiro

-2011
–2011-Home
–2011-Sao Paulo
–2011-Rio de Janeiro
—2011-Rio de Janeiro-Hotel
—2011-Rio de Janeiro-Beach
–2011-Panama

I tried to do the easier I could (2), adding only the yar at the begnnining, but it doesn’t change the name of the folder if there is any folder inside. It’s like “C:\Photos\2011\Rio de Janeiro\Hotel” for example.

This is the “code” I tried. I don’t know how to make it look like a code:

$folders = Get-ChildItem -Path C:\Photos -Directory
$year = "2010" #I would do year by year
ForEach ($item in $folders) {Rename-Item -Path $item.FullName -NewName ($year + " " + $item.Name)}

Thanks in advance for any help!

Bom dia Peterson,

You should use a nested loop … maybe like this:

Get-ChildItem -Path C:\Photos -Directory |
    ForEach-Object {
        $Year = $_.Name
        Get-ChildItem -Path $_.FullName -Directory |
            ForEach-Object {
                Rename-Item -Path $_.FullName -NewName ('{0}_{1}' -f $Year,$_.Name)
            }
    }

… test with “test data” before, please. :wink:

Good Evening (Boa Noite), Olaf!

First of all, thanks a lot for the prompt reply!

Unhappilly, it didn’t work. I got an error at “Rename-Item” line. I’ll paste it here because maybe it helps. I believe that the code is not the problem. I am using PowerShell as administrator, but it still return “access to the path is denied”. My PowerShell is in Portuguese so maybe the translation of some items won’t be perfect.

Rename-Item : Acess to the path 'D:\Photos\2011\Home' was denied.
Line:6 caracter:1
+ Rename-Item -Path $_.FullName -NewName ('{0}_{1}' -f $year,$_.Name)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (D:\Photos\2011\Home:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

It worked for folders without folder inside (child?). If there’s a folder inside a folder, I get this error. If the folder has files but not other folders, it works fine.

So you might have a problem with the given rights on the target folder. Most of the time when the message is “Access denied” you don’t have access. :wink: I just tested it and it worked just as expected. This is the before:

D:.
+---2010
|   +---Panama
|   +---Rio de Janeiro
|   \---São Paulo
+---2011
|   +---Panama
|   +---Rio de Janeiro
|   \---São Paulo
+---2012
|   +---a casa
|   +---Panama
|   +---Rio de Janeiro
|   \---São Paulo
\---2013
    +---Panama
    +---Rio de Janeiro
    \---São Paulo

… and this is the after:

D:.
+---2010
|   +---2010_Panama
|   +---2010_Rio de Janeiro
|   \---2010_São Paulo
+---2011
|   +---2011_Panama
|   +---2011_Rio de Janeiro
|   \---2011_São Paulo
+---2012
|   +---2012_a casa
|   +---2012_Panama
|   +---2012_Rio de Janeiro
|   \---2012_São Paulo
\---2013
    +---2013_Panama
    +---2013_Rio de Janeiro
    \---2013_São Paulo

It should rename the folders even if they have many descendants. Perhaps you don’t have permission to one of the sub folders/files or perhaps one of the paths exceeds 256 characters?

Could you do just one more thing for me? It’s only to know what I do have to search for. Instead of this:

D:.
+---2010
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
+---2011
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
+---2012
| +---a casa
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
\---2013
+---Panama
+---Rio de Janeiro
\---São Paulo

Consider this one. Just another 2 folders inside one of the folders (my first example wasn’t clear because of the “- - -”). Inside “C:\São Paulo” there are 2 more folders named “Beach” and “Home”. It will be D:\Photos\2011\São Paulo\Home and D:\Photos\2011\São Paulo\Beach.

D:.
+---2010
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
||+---Home
||\---Beach
+---2011
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
+---2012
| +---a casa
| +---Panama
| +---Rio de Janeiro
| \---São Paulo
\---2013
+---Panama
+---Rio de Janeiro
\---São Paulo

It works fine for me if I don’t have these 2 folders inside “São Paulo”, but when I create them, it gives me this access error only for the folder with other folder inside. There are no files inside them. I used the tip these folder are only for test purposes.

And… this limation of 256 characters is only for one level name, right? I mean, the whole path (“D:\Photo\Place\Location…”) can be more than 256, right?

Before:

-–photos
±–2010
| ±–Berlin
| | ±–Strand
| | | -–Wannsee
| | -–zu Hause
| | -–Prenzlauer Berg
| ±–Frankfurt
| | -–Boerse
| -–São Paulo
| ±–a casa
| -–praya
±–2011
-–2012

After:
-–photos
±–2010
| ±–2010_Berlin
| | ±–Strand
| | | -–Wannsee
| | -–zu Hause
| | -–Prenzlauer Berg
| ±–2010_Frankfurt
| | -–Boerse
| -–2010_São Paulo
| ±–a casa
| -–praya
±–2011
-–2012