# Need help with the code to create a signle data file out of 1,200 txt's

#### kiton

##### Member
Hello dear forum members,

My data include roughly 1,200 *.txt files, where (a) * is a 3 to 7 digit non-consecutive number representing participant's id, and (b) the contents of the file is participant's response -- text ranging from 5 to 500 words.

What will be the code to create a single data file with column 1 = participant's id (i.e., * -- name of the file), and column 2 = content from that file.

Thankfully,
kiton

#### kiton

##### Member
Just in case

// Working directory presumed to contain only the relevant *.txt files
local flist: dir . files "*.txt"
local nfiles: word count flist'
// Create a data file with these filenames as variables.
set obs nfiles'
gen fname = ""
forval i = 1/=_N' {
local nextfile : word i' of flist'
qui replace fname = "nextfile'" if (_n == `i')
}
compress
gen str id = subinstr(fname, "*.txt", "", .) // pick off the id
// Now read all the input files with one command!