Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there an easy way to rename all the files in a directory? Is PowerShell the way to go?

I'd like to go from this:

c:\family_pics\
   img123.jpg
   img124.jpg

to this:

c:\family_pics\ 
   family_pics_1.jpg 
   family_pics_2.jpg

Ideally, I'd like to run a script on a directory which would rename all the files based the current directory's name while preserving the extension and would walk the directory tree recursively.

share|improve this question

closed as off-topic by Jenny D, Dave M, Tim Brigham, mdpc, Tom O'Connor Mar 21 at 9:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – Jenny D, Dave M, Tim Brigham, mdpc, Tom O'Connor
If this question can be reworded to fit the rules in the help center, please edit the question.

8 Answers 8

up vote 16 down vote accepted

On the basis that everything can be done in one line of PowerShell (just as everything can also be done in 1 line of Perl), here's the PS code for a single directory.

$i=0; get-childitem $dirname | foreach-object {

       rename-item "$dirname\$_" $("{0}_{1}{2}" -f $dirname, ++$i, $_.Extension)
}

To do it recursively, you'd wrap the above in a function and then call it on every directory in your tree. You could also replace the format string with something a bit more sophisticated if you need to use regexes.

share|improve this answer

ReNamer: http://www.den4b.com/

Also see: How to Rename Part of a File Name from a Selection of Files in Windows?

Drop your folder with pictures into ReNamer (subfolders will be added recursively by default). Click on Name column to sort by filename if you want maintain the sequential order. Special meta tag will allow you to use the parent folder name for renaming. Use the following renaming rules:

  1. Delete from Position 1 until the End (skip extension)
  2. Insert ":File_FolderName:_" as Prefix (skip extension)
  3. Serialize Incremental from 1 step 1 repeat 1 as Suffix (skip extension)

enter image description here

share|improve this answer
    
+1 rename was just awesome! –  Chris Marisic Aug 23 '12 at 16:22

Although you can do stuff with a batch file I've found that more control, options etc. and a visual clue of what is about to take place is better.

There's a free tool called CKRename. The original author's site seems to have disapeared but the app is fully functional and quite popular (i've been using it for 8 years). Download CKRename

Here's a screenshot of it in action enter image description here

share|improve this answer

this strips the last four characters (usually the extension) off

dir *.jpg | % { ren $_.Name $_.Name.Substring(0,$_.Name.Length-4) }

You may need to adapt the above to handle spaces or your specific renaming requirements

share|improve this answer

Download RenameFiles (freeware). It's one of the only programs I really miss since I moved from Windows to Apple.

It looks like it's available here http://it-falke.com/software/renamefiles/us_description.php but I don't have a PC to test that link. The last version I used was 2.1.4. It was really simple but versatile.

share|improve this answer

Total Commander has some cool multi-file rename features.

Of course if you are into powershell owenevans00 solution looks great.

share|improve this answer

You could just highlight all the ones you want in explorer then right click on the first one and rename it and windows will then rename them all for you with the same naming convention.

share|improve this answer
    
Just tried it starting with fileA.m, fileB.m, etc. Istead of fileA_001.m, fileB_002.m, instead got fileA_001.m, fileA_001 (1).m, fileA_001 (2).m, etc. –  jacobsee May 8 '09 at 15:48

This is a bit hacky, but I've done this for like 12 years and I'm pretty fast at it:

  • Open a command prompt in c:\family_pics and type dir >> dir.xls
  • Open the dir.xls file in Excel
  • Highlight the first column, select "Text to Columns", and add column breaks on the space characters (ignoring duplicates)
  • You now have a nice table with your current filenames, file sizes, etc
  • Add a new column "ren" before the filename
  • Add a new column after the filename, and use whatever Excel functions you want to generate the new filename. In your case, if you type the first two filenames and then double-click the bottom right corner of the cell, Excel will automagically detect the pattern and generate the remaining names.
  • Save as ren.bat
  • Run the batch file you just created and it will rename all of your directories

Although it may not be immediately obvious, Excel is very well-suited to this kind of task, because you can use any kind of formula you want for generating the new files, using inputs from the existing files (timestamp, filename, size, etc).

share|improve this answer
    
Excel is the best code/script generator I know. –  GvS May 8 '09 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.