birisix
birisix

Reputation: 13

Changing some file extensions in a folder

For example, I want to change all files' extensions to jpg. I can do this with below code. But it is necessary to determine non-jpg extensions one by one. How can i do this, automatically?

cd C:\Users\user\Desktop\
rename *txt *.jpg

Upvotes: 0

Views: 399

Answers (2)

Tomas Forsman
Tomas Forsman

Reputation: 26

After it was pointed it out I realise you might want to make all files jpg files and not just .txt files. One approach would be:

forfiles /M *.* /C "cmd /c if /i not @ext==\"jpg\" copy @file @fname.jpg /Y & del @file"

If you want to include subdirectories:

forfiles /S /M *.* /C "cmd /c if /i not @ext==\"jpg\" copy @file @fname.jpg /Y & del @file"

Original answer:

In a bat file you can do this:

xcopy *.txt*.jpg /Y
del *.txt

Or as a command:

xcopy *.txt *.jpg /Y && del *.txt

Upvotes: 0

user7818749
user7818749

Reputation:

You have 2 options, either skip files that already exist, or rename files that already:

Skip files that already exist.

@echo off
for %%i in ("%userprofile%\desktop\*.*") do if not "%%~xi" == ".jpg" if not exist "%%~ni.jpg" rename "%%~fi" "%%~ni.jpg"

or replace the file if it exists:

@echo off
for %%i in ("%userprofile%\desktop\*.*") do if not "%%~xi" == ".jpg" move /Y "%%~fi" "%%~dpni.jpg"

Upvotes: 1

Related Questions