Script to Rename Many .ndf Physical Files

At my job, I have a large database that's partitioned and has about 115 separate .ndf files. Recently, when I restored it on our DR site, I didn't do it correctly and so instead of .ndf files named like this:

In addition to this, many of the files are on different drives. For example, the files for Services are on drive I: while the files for Events are on drive F:

Now, this database is well over 300GB in size and even with SQL 2008 backup compression the .BAK file is still 43GB. I was not looking forward to copying it across the country again to redo the restore so I could get the filenames right.

Forturnately, I am consistent in my naming of the filegroups, though. They are named thusly:

So I created a script that will take the filegroup names and rename the files for you. What it does is create 2 scripts you can run. The first one is a set of ALTER DATABASE commands to rename how the files will appear in the system tables. The second thing it spits out is a set of DOS commands you can run from a batch file to rename the physical files.

What you wind up with after all is said and done, are .ndf files named like this: