I'm trying to write a short program that does the following over and over again.
- Import all files in a folder
- Automatically generate names for the imported files based on source file name.
- Perform a simple operation on the file.
- Re-Export the modified files with a new name.
For concreteness, here's an example of how I would do this manually. It is important that I be able to have a descriptive name for the files (e.g. file1 etc).
root = NotebookDirectory[];
file1 = Import[root <> "file1.csv"];
file2 = Import[root <> "file2.csv"];
These files are just .csv files.
file1 is {{1, 1}, {2, 9}, {3, 3}, {4, 2}, {5, 9}, {6, 1}, {7, 6}}
file2 is {{1, 6}, {2, 0}, {3, 4}, {4, 7}, {5, 10}, {6, 9}, {7, 3}}
Then I would modify the files. I've defined a simple function that adds a shift to each column of the table with name 'FILEname':
MODdata[FILEname_] :=
Table[{FILEname[[i]][[1]] + 2, FILEname[[i]][[2]] + 10}, {i, 1,
Length[FILEname], 1}]
file1mod = MODdata[file1];
file2mod = MODdata[file2];
Then finally I want to export the files with a new name that indicates that they've been modified.
Export[root <> "file1_mod.csv", file1mod]
Export[root <> "file2_mod.csv", file2mod]
Any help with this would be greatly appreciated!
......... EDIT ............
I tried to adapt @TumbiSapichu suggestion to my case but am still having issues. In the code below I replaced myAnalysedData=readCurrentCSV+1
with my "MODdata" function above:
root = NotebookDirectory[];
myCurrentCSVs = FileNames[root <> "*.csv"];
i = 1;
While[i <= Length[myCurrentCSVs],
readCurrentCSV = Import[myCurrentCSVs[[i]]];
(*Make some operation on the data,say,add 1*)
myAnalysedData =
Table[{readCurrentCSV[[k]][[1]] + 2,
readCurrentCSV[[k]][[2]] + 10}, {k, 1, Length[readCurrentCSV], 1}];
(*Now,export the analysed data to new file*)
Export[StringJoin["Analyzed", myCurrentCSVs[[i]]], myAnalysedData];
i++]
I'm getting both an 'Export: Directory xxxx does not exist' and an 'OpenWrite' Cannot open xxxx/file1.csv' error for both files. Maybe I'm not specifying the file paths correctly with my root = NotebookDirectory[]
?
NotebookDirectory[]
? $\endgroup$MODdata
:modify[data_] := {First@# + 2, Last@# + 10} & /@ data
. $\endgroup$