A simple solution is:
@echo off
if exist "file.txt" %SystemRoot%\System32\findstr.exe /R "^." "file.txt" >"file.tmp"
if exist "file.tmp" for %%I in ("file.tmp") do if %%~zI == 0 (del "file.tmp") else move /Y "file.tmp" "file.txt"
FINDSTR runs a regular expression find for lines which have at least one character at the beginning of the line and so FINDSTR outputs all lines not being empty. The output by FINDSTR is redirected into temporary file file.tmp.
The created temporary file replaces the input file on being created at all (input file exists) and is not empty which means at least one non-empty line found in input file. The temporary file is deleted on being created, but being an empty file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
del /?
echo /?
findstr /?
for /?
if /?
move /?
See also the Microsoft documentation about Using command redirection operators for an explanation of the redirection operator >.
(for /F "delims=" %a in (file.txt) do if {%a}=={} (echo %a)) > newfile.txtwork?DOSdoesn't havefor /f. Are you speaking ot the Windows Command Line (cmd) instead? (then please adapt the tags) Also within a batch file, you need%%a.%ais directly on the command line only (bothDOSandcmd)for /falready ignores empty lines, so there is no need foriffindstr /v "^$" file.txt(not inDOSthough)@FOR /F useback^ tokens^=*^ delims^=^ eol^= %L in ("test.txt") do @echo(%L, lines over 8191 characters will be skipped.