Capturing the AVR hex file built by Arduino on Windows

One often sees enquiries by people trying to save the hex file made during the Arduino build process.

It is not trivial, as in their wisdom, Arduino hides these details, and builds the hex file in a randomly named temporary directory for each IDE which it deletes when the IDE is closed.

There are times when you may want to save the hex file, perhaps to load it without a bootloader or using a non-supported bootloader, Flashing LED driver using an ESC was just such a project.

The following is a batch file that should run on most modern Windows systems. It will find the most recent build and copy the hex file to the current directory.

@echo off
rem generate a unique workfile name
set /a workfile=%RANDOM%+100000
set workfile=work-%workfile:~-4%.tmp
echo rand is %workfile%
if exist %workfile% goto tryworkfileagain
rem get directory listing
dir /ad /od /b %temp%\build*.tmp >%workfile%
rem get the last entry 
for /F %%f in (%workfile%) do set build=%%f
del %workfile%
echo build at %build%
copy %temp%\%build%\*.hex .

If you have two or more IDEs open concurrently, the file copied will the most recent build, make sure you build for the hex you want immediately before running the batch file.

PS: I still do not understand why Arduino does not place the build files in a sudirectory of the source / project file… like almost every other IDE.