1
$\begingroup$

I have two questions. I am trying to export results from my calculation when I iterate. I am processing multiple files using code below:

processFile[fileName_] :=
 Module[{data},
  data = Import[fileName, "Data"];
  (*Here I have all my function and processing that I don't want to include here*)
  Results1 = Table[{(ii 5) - 2.5, Abs[Mean[bin[5, ii]]]}, {ii, 1, 200}];
  (*then here I want to export the resulting table*)
  Export["Results_" <> fileName <> ".csv" , Results1]

When I want to loop with multiple files I use this command :

files = FileNames[NotebookDirectory[] <> "*.txt"];
Do[Print["Processing file ", files[[n]]];
processFile[files[[n]]], {n, 1, 2}]

I got an error, because in Export command fileName should come before "Results_", so I changed it to

Export[fileName <> "Results_" <> ".csv" , Results1]

It gives no error, but I wonder if there is a way to fix this issue without moving fileName?

Also As you see the files that I import are .txt, but I want to export in .csv,

When I export, the exported files contain .txt attached, for example ("file1.txtResults.csv") How to get rid of .txt in the name?

Thank you,

$\endgroup$
5
  • $\begingroup$ <> is a concatenation operator, so "file1.txtResults.csv" is exactly what you asked for in "Results_" <> fileName <> ".csv". You likely want to remove the "txt" in your loop. $\endgroup$
    – dwa
    Commented Apr 3, 2020 at 2:07
  • $\begingroup$ if i remove .txt it will read all the files, and that is not what i want $\endgroup$
    – Erfi
    Commented Apr 3, 2020 at 2:19
  • 2
    $\begingroup$ I think you want Export["Results_" <> FileBaseName@fileName <> ".csv" , Results1] $\endgroup$ Commented Apr 3, 2020 at 3:04
  • $\begingroup$ You are mixing contexts. In your module, you need to strip the "txt". Rohit has suggested one way. $\endgroup$
    – dwa
    Commented Apr 5, 2020 at 1:00
  • $\begingroup$ Thanks @RohitNamjoshi it worked $\endgroup$
    – Erfi
    Commented Apr 7, 2020 at 21:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.