Search code examples
filebatch-filedirectorybatch-rename

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

Solution

  • 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"