2

Basically I have a number of folders all called different names. I would like to add a suffix to the end of each folder and within Windows (no 3rd party software).

For example: Mercury, Venus, Earth, Mars

And I need: Mercury 2015, Venus 2015, Earth 2015, Mars 2015

Luca
  • 21
  • 1
    Are all the folders under a common root directory ? Are all the folders within the root directory to be renamed ? You can achieve that easily with a PowerShell script. – Ob1lan May 28 '15 at 12:02

1 Answers1

5

This can be done easily with Powershell:

Get-ChildItem -Directory | ForEach {Rename-Item $_ "$_ 2015"}

if you want to do this recursively down a folder tree (sub folders) add "-Recurse" after -directory.

What this essentially does is:

  • Get-ChildItem (Get all items in current folder)
  • -Directory (limit search to folders)
  • | Pipe (send) results to next command
  • ForEach {} (For each folder found)
  • Rename Item $_ "$_ 2015" (Rename the folder to the same name with " 2015" on the end)

In this case $_ is the curent object sent fromthe first command to the second (list of folders), and then represents each sub-object (each folder) inside the ForEach.

Remember to cd to your base folder first else you'llbe trying to rename the wrong folder.. and if you execute this within C:\windows\system32 or similar, you're in trouble! (cd C:\users\me\documents\top_folder)

This is tested on windows 7, but the syntax may be slightly different for other windows versions. If you can tell me which version of windows you have, I'll give you another one-liner to use.

Also.. bear in mind that if you run it repeatedly, you'll end up with "my folder 2015 2015 2015 2015 2015" and it'll be a pain to clean up

Hope this helps

Fazer87
  • 12,660
  • Nice answer, but the use of 'foreach' in this case is not necessary. This command should be sufficient : Get-ChildItem -Directory | Rename-Item -NewName {$_.Name + " 2015"} – Ob1lan May 28 '15 at 12:17
  • True, but by adding in a Foreach, its very simple to explain to someone who is not PowerShell Savvy enough to work it out on their own iommediately. It also gives a good base to work from if the user wants to expand this and do other stuff to the files as well. It's also given a good base to wrap the rename in an IF statement or similar, should the user want to qualify which folders to rename. – Fazer87 May 28 '15 at 12:19
  • No problem Luca. If this answer helped solve your problem, please mark it as the accepted answer and/or upvote it so that other users know this worked. – Fazer87 May 28 '15 at 14:03