How can I create a directory in a notebook directory and export a file (resulted from that notebook) to created subdirectory?
2 Answers
I suppose that by "a directory in a notebook directory" you mean "the directory of the notebook where the Export is evaluated". Of course the notebook should first be saved somewhere. Then do :
Export[FileNameJoin[{NotebookDirectory[], "newDirectory",
"testFile55.jpg"}], ConstantImage[Red]]
The "newDirectory" wil be created as part of the file creation (no need to create the directory separately first).
The Mathematica Version should not be to old.
-
2$\begingroup$ The option
CreateIntermediateDirectories
is used to control the behavior of whether subdirectories need to be explicitly created $\endgroup$– b3m2a1Commented Mar 27, 2021 at 22:48 -
$\begingroup$ It shows as - Export::nodir: Directory C:\Users\v\Desktop\export mathematica\newDirectory\ does not exist. $\endgroup$– mrt31Commented Mar 27, 2021 at 23:03
-
$\begingroup$ @MalliTangi: Which version do you use? As andre314 has mentioned,the Mathematica version should not be too old for this to work. For older versions you had to create the directory yourself. That would then look like this:
filename = FileNameJoin[{NotebookDirectory[], "subdir", "test.jpg"}]; CreateDirectory[DirectoryName[filename], CreateIntermediateDirectories -> True]; Export[filename,data];
. By the way, the extraCreateDirectory
does of course no harm in newer versions as well... $\endgroup$ Commented Apr 1, 2021 at 16:08 -
$\begingroup$ @ Albert, thanks. I use version 12. The code works when I create a directory myself. :) $\endgroup$– mrt31Commented Apr 2, 2021 at 12:34
ClearAll[random];
SeedRandom[12];
random = RandomInteger[{0, 10}, {5, 5}];
Export["directory like C:\\Documents\\trial.xls", random, "xls"];
This should export random
matrix to Documents
directory with a file name trial.xls
.