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
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
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%
)
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.