Quick Links
Bible Search Christian Links
Online Bibles Link to Us
  Downloads Web Hosting  
  Domain Names  


PDA

View Full Version : BATCH - Massive File Extension Renaming


Master Jake
Jun 14th 2009, 05:13 AM
OK, so I'm a strong DOS Command Prompt user. I've even went to the extent of creating full RPG games using Batch Files, which is what Master Jake is all about.

Today, someone sent me a ton of files and to execute them, I needed to change all their extensions from ".txt" to ".bat" without renaming the actual filename. I hadn't done this before, surprisingly, so I went into cmd and studied up on the for loop because I knew I would be needing it.

Needless to say, I figured it out and am here to spread the word incase you ever come across something like this.

-----

When Will I Need This?
If you ever need to change file extensions for hundreds of files from say ".png" to ".jpg" and don't want to manually rename them all.

How I Do It Then?

First, you'll need to make sure all the files you want to extension change are in their own directory, otherwise you'll have to do some other checking which I'm not going to be showing.

Open notepad.

We'll start by turning echo off, cause we don't need the directories all up in our grill.


@echo off


next, we create the for loop. We need to loop through every file in the current directory and rename it from whatever it currently is, to whatever it currently is ".newextension". The extension we will be using is ".bat" because In my example, I needed to change many ".txt" files to ".bat" files so I could run them.

To do this, make a simple for loop. The main thing to keep in mind during this for loop is that the:

%%~nD returns the filename of the file itself (without the extension) which is exactly what we need in this case.


@echo off

for %%D in (*) do rename %%D %%~nD.bat
pause


We added that simple pause at the end so we can view the results in cmd when we are done. After that, just save the file as "something.bat", drag it into the directory full of files to change, and run it. Press any key after it has completed, and WAHLAH. All your file's extensions have been successfully changed without renaming the files themselves.

Simple, and fast. Amazing! Enjoy!

bdh
Jun 14th 2009, 06:00 AM
I needed to change all their extensions from ".txt" to ".bat" without renaming the actual filename.Why not just use the ren command like so

ren *.txt *.bat

Master Jake
Jun 14th 2009, 06:09 AM
Why not just use the ren command like so

ren *.txt *.bat

... :cry: I overlooked that. Why must I over complicate things, I do that all the time.

Can you add stupidity to the OPSEC and remove this thread because of that.

Master Jake
Jun 14th 2009, 07:05 AM
LOL... I searched this and there are actually people using even more complicated methods than mine to do the exact same thing. At least I'm not the only stupid one :)

SA Topsites