Terra Vita
Terra Vita

Reputation: 23

Append Filename at the beginning of each text file in a given folder

So I have a folder with hundreds of OCR output text files. I'm trying to make a batch file which will append the filename at the beginning of every file.

So far, I looked thru and found this code on stackoverflow, but it appends the filename at the beginning of each line. Appending the filename beginning of each line

    @echo off
if "%~1" equ ":FindFiles" goto :FindFiles

cd "C:\Users\Neha\Desktop\GB PRoducts\OCR\test"

:: Append newline to text files that are missing newline on last line
for /f "eol=: delims=" %%F in ('"%~f0" :FindFiles') do echo(>>"%%F"

:: Merge the text files and prefix each line with file name
findstr "^" *.txt >output.log

exit /b


:FindFiles
setlocal enableDelayedExpansion

:: Define LF to contain a newline character
set lf=^


:: The above 2 blank lines are critical - do not remove

:: List files that are missing newline on last line
findstr /vm "!lf!" *.txt

Upvotes: 1

Views: 3287

Answers (1)

Stephan
Stephan

Reputation: 56238

either

type *.txt>x.x 2>&1

(type writes the filenames to STDERR and the file content to STDOUT. >output.log 2>&1 writes STDOUT to the file and STDERR to the same destination.)

or

for %%a in (*.txt) do (echo %%a&type "%%a")

Upvotes: 1

Related Questions