Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
3  
What operating systems do you have available? I think something like this will be much easier on linux or OS X than windows. –  Darth Android Jun 22 '12 at 14:17
    
I agree, a bash script could probably handle this quite nicely. –  Sion Jun 22 '12 at 14:22
add comment

2 Answers

up vote 3 down vote accepted

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%
)
share|improve this answer
add comment

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.

share|improve this answer
    
awesome, thanks guys!! –  Joldfield101 Jun 25 '12 at 9:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.