Please read my answer on How to read and print contents of text file line by line?
Then the code of the batch file should be clear which is working for all ANSI, OEM and UTF-8 encoded text files with lines with line number and colon prepended on each line by FINDSTR not longer than 8184 characters.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=%~1"
if not defined SourceFile set "SourceFile=file.txt"
if not exist "%SourceFile%" echo ERROR: File "%SourceFile%" not found!& exit /B 1
set "Line="
for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%SourceFile%" 2^>nul') do (
setlocal EnableDelayedExpansion
if defined Line (echo(!Line:*:=!>>"%SourceFile%") else del "%SourceFile%"
endlocal
set "Line=%%I"
)
if exist "%SourceFile%" for %%I in ("%SourceFile%") do if %%~zI == 0 del "%SourceFile%"
endlocal
The source file of which name is passed to the batch file with the first argument string or as specified in the batch file is deleted if it contains just one line.
The source file is also deleted if it is an empty file, i.e. is a file with a file size equal 0. That is done by the last but one line of the batch file.
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 /?
... explains %~1
... first argument with surrounding "
removed.
del /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
if /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of the operator &
as used on fifth line.