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.
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.
Code:
@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!
Bookmarks