Change extensions of exe, com, and bat

1.    Save

 

@echo off

setlocal enabledelayedexpansion

 

rem Iterate through all subfolders

for /r /d %%i in (*) do (

    rem Check if there are .bat, .com, .exe files in the current folder

    dir /b "%%i\*.bat" >nul 2>nul

    if not errorlevel 1 (

        rem Change .bat files to .baa and save original filenames in changes.txt

        pushd "%%i"

        for %%f in (*.bat) do (

            if "%%~nxf" neq "change.bat" (

                if "%%~nxf" neq "recover.bat" (

                    set "old_name=%%~nf%%~xf"

                    set "new_name=%%~nf.baa"

                    ren "%%f" "!new_name!"

                    echo Renamed '!old_name!' to '!new_name!' >> changes.txt

                )

            )

        )

        popd

    )

    dir /b "%%i\*.com" >nul 2>nul

    if not errorlevel 1 (

        rem Change .com files to .caa and save original filenames in changes.txt

        pushd "%%i"

        for %%f in (*.com) do (

            if "%%~nxf" neq "change.bat" (

                set "old_name=%%~nf%%~xf"

                set "new_name=%%~nf.caa"

                ren "%%f" "!new_name!"

                echo Renamed '!old_name!' to '!new_name!' >> changes.txt

            )

        )

        popd

    )

    dir /b "%%i\*.exe" >nul 2>nul

    if not errorlevel 1 (

        rem Change .exe files to .eaa and save original filenames in changes.txt

        pushd "%%i"

        for %%f in (*.exe) do (

            if "%%~nxf" neq "change.bat" (

                set "old_name=%%~nf%%~xf"

                set "new_name=%%~nf.eaa"

                ren "%%f" "!new_name!"

                echo Renamed '!old_name!' to '!new_name!' >> changes.txt

            )

        )

        popd

    )

)

 

echo Done renaming files.

 

 

 

2.    To recover the original files.

@echo OFF

SETLOCAL enableextensions enabledelayedexpansion

 

for /r /d %%i in (*) do (

    rem check if file exists

    if exist "%%i\changes.txt" (

        rem move to the directory

        pushd "%%i"

 

        rem tokenizing for one

        for /f "tokens=1 delims=" %%a in (changes.txt) do (

            rem Replace single quotes with a temporary delimiter (^)

            set "line=abc%%axyz"

            set "line=!line:'=^!"

 

            rem Extract the substring between the temporary delimiters

            for /f "tokens=2, 4 delims=^" %%b in ("!line!") do (

                rem original extension

                set "SUBSTR1=%%b"

                rem changed file name

                set "SUBSTR2=%%c"

 

                rem Undo the renaming

                ren "!SUBSTR2!" "!SUBSTR1!"

            )

        )

        DEL changes.txt

        popd

    )

)

 

echo Done restoring files.