# Batch renaming file extensions



## Fractalogic (Jul 4, 2010)

Hey all!

I have a bunch of files in a folder and I need to set the correct file extensions for these files.

They actually use more than one extension. The file names consist of a prefix, a infix and a suffix. So it's like two file extensions one after another. The infix is the actual extension for the file type, while the last extension or the "suffix" is an appended temporary extension.

The file name scheme looks like this.

```
0001 IMG - Description [year].jpg.tmp
```
The appended ones needed to be removed in order for the right software to be associated to the file type. I used the REN command in CMD to remove it.


```
ren *.jpg.tmp *.jpg
```
After doing this the file names changed to something like this:

0001 IMG - Description [year].jpg.jpg

So although the files were now associated to the right software they had a double file extension (jpg). So I used the REN command once again.


```
ren *.jpg.jpg *.
```
After doing this most files were now set and ready, with only one file extension as I wanted. But some of them now don't have a file extension at all.

My question is, how do I add the file extension to those files who don't have any extension? They all have different names, so have do you "select" all those who don't have a file extension and set a new extension for these?


----------



## Fractalogic (Jul 4, 2010)

The files that still need an extension have a file name beginning with a number, like this.


```
0004
0005
0052
0059
0063
0064
0065
0071
0086
......
```
How can I target these files for file rename command?

They don't have a file extension anymore. And the year values at the end of the file name are different for most of the files. So they don't have any text string in common so to speak. I mean it doesn't say like "2008" for each and every one of them. Because that would be easier. You could just use wildcard characters to specify only files with the "2008" in the name. But you also need to consider that this might affect the other filed which already have a file name extension, should they contain "2008" in their name as well. But I guess you can overcome this by temporarily moving those files to a different folder.

Any ideas? Thoughts?

Can't I just type in something like this:


```
ren 0005*; 0052*; 0059 *.jpg
```
That doesn't look right, I know. But to use a command similar to this would be great.


----------



## Squashman (Apr 4, 2003)

Well as far as your first post goes, this is what I would have done.

Batch file

```
@echo off
For /f "tokens=* delims=" %%I in ('dir /a-d /b *.tmp') do rename "%%~I" "%%~nI"
```
Output

```
C:\Users\Squashman\batch\rename>dir /a-d /b *.tmp
image1.jpg.tmp
imgag2.jpg.tmp

C:\Users\Squashman\batch\rename>extension.bat

C:\Users\Squashman\batch\rename>dir /a-d /b *.jpg
image1.jpg
imgag2.jpg

C:\Users\Squashman\batch\rename>
```
Now with your 2nd post you are saying there is no extension at all on the file names? If that is the case the file extension modifier should be null or not defined. As long as there are no periods at all in the file names you could do something like this. There can be no periods. The command modifiers really don't know what the file extension is. It just works backwards with the file name until it finds a period. So if I have a file named Image1.Squashman, the batch file will tell me that Squashman is the file extension.

Batch

```
@echo off
For /f "tokens=* delims=" %%I in ('dir /a-d /b *') do IF "%%~xI"=="" rename "%%~I" "%%~I.jpg"
```
Output

```
C:\Users\Squashman\batch\rename>dir /b
extension.bat
image1.jpg
Image3
image4
imgag2.jpg
No_extension.bat

C:\Users\Squashman\batch\rename>No_extension.bat

C:\Users\Squashman\batch\rename>dir /b
extension.bat
image1.jpg
Image3.jpg
image4.jpg
imgag2.jpg
No_extension.bat

C:\Users\Squashman\batch\rename>
```


----------

