0
$\begingroup$

How can I create a directory in a notebook directory and export a file (resulted from that notebook) to created subdirectory?

$\endgroup$

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
4
  • 2
    $\begingroup$ The option CreateIntermediateDirectories is used to control the behavior of whether subdirectories need to be explicitly created $\endgroup$
    – b3m2a1
    Commented Mar 27, 2021 at 22:48
  • $\begingroup$ It shows as - Export::nodir: Directory C:\Users\v\Desktop\export mathematica\newDirectory\ does not exist. $\endgroup$
    – mrt31
    Commented 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 extra CreateDirectory 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$
    – mrt31
    Commented Apr 2, 2021 at 12:34
0
$\begingroup$
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.

$\endgroup$

Your Answer

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

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