9
votes

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.

16
votes

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.

10
votes

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

  • Excel is the best code/script generator I know. – GvS May 8 '09 at 14:34
3
votes

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

1
vote

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.

  • 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
1
vote

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

  • +1 rename was just awesome! – Chris Marisic Aug 23 '12 at 16:22
0
votes

Total Commander has some cool multi-file rename features.

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

0
votes

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.

0
votes

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

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