# Rename-Item for not empty folders

**URL:** <https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376>\
**Category:** PowerShell Help\
**Created:** [May 13, 2020, 11:16pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376 "2020-05-13T23:16:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![petersonribeiro](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@petersonribeiro](https://forums.powershell.org/u/petersonribeiro)\
**Post date:** [May 13, 2020, 11:16pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/1 "2020-05-13T23:16:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 14, 2020, 1:47am UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/2 "2020-05-14T01:47:29Z")

</div>

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. 😉

---

<div class="post-metadata">

**Author:** ![petersonribeiro](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@petersonribeiro](https://forums.powershell.org/u/petersonribeiro)\
**Post date:** [May 14, 2020, 4:24pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/3 "2020-05-14T16:24:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 14, 2020, 5:27pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/4 "2020-05-14T17:27:45Z")

</div>

> [@](#):
>
> but it still return “access to the path is denied”

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. 😉 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
```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 14, 2020, 6:36pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/5 "2020-05-14T18:36:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![petersonribeiro](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@petersonribeiro](https://forums.powershell.org/u/petersonribeiro)\
**Post date:** [May 14, 2020, 7:16pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/6 "2020-05-14T19:16:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 15, 2020, 1:17am UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/7 "2020-05-15T01:17:27Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/rename-item-for-not-empty-folders/14376/8 "2024-05-16T20:29:40Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


