Rename filename -- add prefix with date

March 28, 2011 at 19:46:30
Specs: Windows Vista
Hi there,

I have a group of html files that need to be renamed. Each html file contains a blog post. The file begins with the title of the blog post, then a break, then "Originally posted Month xx, yyyy". What I need is to take the "xx" value and put it in front of the current name. In other words, using VBS I want to add that prefix to every file within a certain folder.

Here is the html code of one of the files:

<?xml version="1.0" encoding="UTF-8"?>
<title>Brains Brains Brains - October 25th, 2004</title>
<h1><a hre/f="">Brains Brains Brains</h1>
<small>Originally posted October 25th, 2004</small>

Looking at the code above, I see now that the date is also included between the <title> tags. Perhaps that could make the VBS script simpler to make?

I've taken a look at example #3 on this tutorial, but seeing as I know hardly any VBS, I can't customize it for my own situation.

See More: Rename filename -- add prefix with date

Report •

March 28, 2011 at 21:02:58
If you have a choice, here's a Ruby for Windows script

Dir["*.html"].each do |file|
   f.each do |line|
      day=line.scan(/Originally\s+posted\s+\w+\s+(\w+),/)[0][0] if line[/small/]
   newname = day+"_"+file
   File.rename( file  , newname )

to run it ,

C:\work>ruby  rename_script.rb

Report •

March 28, 2011 at 21:27:09
That worked really well! Is there any chance that it could also include the year and month, in number format? And without the "th" as in 14th?

So something for December 13, 2004 would look like:


Changing the underscore to the dash should be easy enough, but is it possible to convert the month (as a word) into a number (01 through 12)?

Report •

March 28, 2011 at 21:47:00
I suggest you post a list of possible date scenarios in your html files. I don't want to do it and then you keep posting changes. Define once and for all your requirements clearly as far as possible.

Report •

Related Solutions

March 28, 2011 at 22:08:26
Right, okay.

Years go from 2005 through 2010. Months go from January through December. Dates go from the 1st to the the 31st.

The names must be:


Example: 2004-01-05-original-post-title.html

I want the year to always be the full year (2004, not 04). I want the month and the dates to be two-digit numbers. If the month or the date is originally a single digit (1 to 9), it must be preceded by a zero. In other words: 01, 02, 03, 04 ... 10, 11, 12, etc.

As shown in the example, dashes must be between the year, month, and day, and the original title. There is also no "th" after the day number.

Good example: 2004-01-05-original-post-title.html
Bad example: 2004-01-05th-original-post-title.html

Sometimes there are two blog posts on the same day. I've tested your original code out and it works fine when there are two identical dates. The result are two files that look like this:


Because the post titles are different, there will never be a completely identical name, even if the date is the same. This suits me fine.

Those are all the date scenarios. Any days between January 1st, 2005 up to December 31st, 2010. If you wish to see a list of all the posts and their dates, it's available at

Thanks for your efforts. I really do appreciate it.

Report •

March 28, 2011 at 22:39:29
require 'date'
Dir["*.html"].each do |file|
   f.each do |line|
      m,d,y = line.scan(/Originally\s+posted\s+(\w+)\s+(\w+),(.[^<]*)/)[0] if line[/small/]      
   d = sprintf("%02d", d.gsub!(/[^0-9]/,"") )
   m = DateTime.parse(m).strftime("%m")
   newname = y + "-" + m + "-" + d + "-" + file
   File.rename( file  , newname )

Report •

March 28, 2011 at 22:45:33
Thank you. I've tested it out and it works perfectly. That really is quite amazing! It saves me so much work and it really does turn out exactly the way I want it.

Report •

September 19, 2011 at 07:16:56
any way that this script could edit only the the selected file (s)?

For example, I would either select one file or multiple files than start the script?

My need would be the same as above without dashes


Report •

Ask Question