This batch file can be used for this task.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do for /F usebackq^ delims^=^ eol^= %%A in ("%%I") do >>"%OutputFile%" echo %%~nxI;%%A
endlocal
This batch file writes into file C:\Test\output.txt
all non-empty lines of all *.txt files found in current directory with file name and file extension, but without file path, and a semicolon at beginning of each line.
The current directory should not be C:\Test
on running the batch file above. Otherwise the batch file below with an additional IF would be needed to prevent processing the output file having the same file extension as the files to read and write with file name into each line.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do if /I not "%%~fI" == "%OutputFile%" for /F usebackq^ delims^=^ eol^= %%A in ("%%I") do >>"%OutputFile%" echo %%~nxI;%%A
endlocal
The batch file below could be used for reading in also empty lines and write them into output file with file name and semicolon.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do if /I not "%%~fI" == "%OutputFile%" (
for /F delims^=^ eol^= %%A in ('%SystemRoot%\System32\findstr.exe /N "^" "%%I" 2^>nul') do (
set "Line=%%A"
setlocal EnableDelayedExpansion
>>"%OutputFile%" echo %%~nxI;!Line:*:=!
endlocal
)
)
endlocal
Modify echo %%~nxI;%%A
to echo %%A;%%~nxI
respectively echo %%~nxI;!Line:*:=!
to echo !Line:*:=!;%%~nxI
to have the file name after a semicolon at end of each line in output 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.
del /?
echo /?
endlocal /?
findstr /?
for /?
set /?
setlocal /?
See How to read and print contents of text file line by line? with the full explanation of the used code. It can be seen easily that Windows command processor is designed for executing commands and applications and not for processing text files.