I think you're missing setlocal ENABLEDELAYEDEXPANSION
and you need to use echo !line!
if you want to output inside the block:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set line=0
FOR /f "usebackqdelims=" %%a in ("%filename2%") do (
set /a line = !line!+1
ECHO !line!
if !line!==39 (
echo REPLACED TEXT>>%tempfile%
) else (
echo %%a>>%tempfile%
) )