You could use another FOR loop which calls for each *.spl file a subroutine for renaming the file.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%# in ('dir *.spl /A-D /B 2^>nul') do call :RenameFile "%%#"
endlocal
goto :EOF
:RenameFile
for /F "usebackq delims=" %%I in (%1) do set "FirstLine=%%I" & goto GetNamePart
:GetNamePart
set "NamePart=%FirstLine:~56,5%"
if not exist "%~n1_%NamePart%%~x1" ren %1 "%~n1_%NamePart%%~x1"
goto :EOF
The FOR loop in main code - third line - uses command DIR to get a list of *.spl files before starting the rename operations. It is not advisable to use here just for %%# in (*.spl) do ...
as the files in the current directory are renamed which means the directory entries change while FOR loop is running. It is strongly recommended to get first the complete list of *.spl files using DIR and then process this list independent on the file renames made in the directory.
The main FOR calls the subroutine RenameFile
to process each file from the list.
A file is not renamed if there is already an existing file with same name. This is very unlikely but should be nevertheless checked first.
Please note that the batch code as is does not prevent for appending an underscore and the five characters from first line multiple times to a file name if the batch file is executed more than once on the directory. An additional IF condition would be needed to avoid multiple renames of files.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
ren /?
set /?
setlocal /?
The operator &
as used on FOR loop in subroutine makes it possible to run multiple commands on a single command line, see Single line with multiple commands using Windows batch file for details.