# Elegant ways of solving this file batching task

There are a batch of data files in a folder (dir = "D:\\Documents\\datadir"), each of which consists of a list of data items, one per line. Each data item is a pair with the first component being either 0 or 1.

Now I want to

• import all the data files
• for each data file, extract the second components from the data items with their first components being 1
• export all the data files into a new folder (newdir = "D:\\Documents\\newdatadir"), using their individual original file names.

What elegant ways would you take to solve this file batching task?
In particular, how to use Map to solve it (see my failure below)?

Using Do, I solve it as follows:

Do[
data = Cases[Import[dir <> "\\" <> file, {"Data"}], {x_, _} /; x == 1][[All, 2]];
Export[newdir <> "\\" <> file, data];
,
{file, Import[dir]}
]


Using Map, I can only import and process the data and failed to export them with their individual original file names:

Map[Function[Cases[#, {x_, _} /; x == 1][[All, 2]]], Map[Function[Import[dir <> "\\" <> #, {"Data"}]], Import[dir]]]


Possible test case for data files ("\\" for "\newline"):

data1.txt: 0 20 \\ 1 21 \\ 0 200 \\ 1 211 \\ 1 2111
data2.txt: 1 21 \\ 1 2111 \\ 0 20 \\ 1 21 \\ 0 2000


Related posts:

SetDirectory[dir];