第二个FOR不要DIR了
@echo off
for /f "tokens=*" %%i in ('dir /b /a-d *.txt') do call :lp "%%~ni"
pause
goto :eof
:lp
set n=
setlocal EnableDelayedExpansion
for /f "tokens=1* delims=-" %%i in ("%~1") do (
set fils=%%i
set fils1=!fils!---3.txt
if "%~1.txt" == "!fils1!" goto :eof
if exist "!fils1!" (
:rn
set /a n+=1
set fils1=!fils!!n!---3.txt
if exist "!fils1!" goto rn
)
)
ren "%~1.txt" "!fils1!"