5
$\begingroup$

I am trying to import, analyze, and save all the .txt files in a specific directory. I can do this with a single .txt file using the following code:

data= Import["D:Experiments\\raw-data.txt", "Table"];
range = Select[data, 400 < #[[1]] < 550 &];
smooth = Interpolation[range, InterpolationOrder -> 3];
ex = Table[{x,smooth[x]}, {x, 400, 550, 0.050}];
Export["D:Experiments\\raw-data-out.txt", ex, "Table"];

Could you help me how to perform this process for all the .txt files in my specific directory at once?

$\endgroup$
4
$\begingroup$

How about

SetDirectory["D:Experiments\\"];
names = FileNames["*.txt"];
Do[
    data = Import[name, "Table"];
    range = Select[data, 400 < #[[1]] < 550 &];
    smooth = Interpolation[range, InterpolationOrder -> 3];
    ex = Table[{x,smooth[x]}, {x, 400, 550, 0.050}];
    Export[FileBaseName[name]<>"-out.txt", ex, "Table"];,
{name, names}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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