6

I have 2000 txt files that I need to read the first line of (it should contain a person's name).

Ideally I would have a spreadsheet/csv with

FILENAME, FIRST LINE OF FILE
DavidPostill
  • 162,382

2 Answers2

3

Under Windows, you can do this with a batch file:

@echo off
setlocal enabledelayedexpansion

set OUTPUT=names.csv
del %OUTPUT% > nul 2>&1

for %%f in (*.txt) do (
    set /p NAME=< %%f
    echo %%f,!NAME! >> %OUTPUT%
)

endlocal

If you want the filename without the extension, change the for loop to this:

for %%f in (*.txt) do (
    set /p NAME=< %%f
    set INPUT=%%f
    echo !INPUT:~0,-4!, !NAME! >> %OUTPUT%
)
jftuga
  • 3,265
1

The best I came up with was

$ perl -e 'for(@ARGV){open($f,"<",$_);print"$_,",scalar<$f>}' *.txt
aaa.txt,aaa
bbb.txt,bbb

Perl works on Windows as well as Unix,Linux,etc - but you may need to install it first.