3
$\begingroup$

Suppose I want to Save["filename", foo] under Windows. Ordinarily, the way to deal with file names under windows is to open the Insert dialogue and find a file path, but in this use case, the file is new, so it does not exist (yet). Unlike every other windows program I am familiar with, Mathematica is unwilling to create a new file (that is, once you locate the relevant folder, you type in "mynewfile.m", Mma balks. This seems like a bug (sorry, Dan Lichtblau, a misfeature :)), but is there any reasonable workaround? Am I missing something obvious?

$\endgroup$
4
$\begingroup$

Insert ▶ File Path... only inserts a string that represents the location of a file in the correct formating for your operation system. I also often misuse it to specify a filenames inside functions like Save. In that case I just select any file in the proper folder, and than change the last part of the string to the filename and extension I really want.

You can get a much more convenient and Windows like behavior using

Save[SystemDialogInput["FileSave"], foo]

This also works beautifuly with Export, which is more suitable than Save in most cases, and can be made even more practical by predefining a file path (second input to SystemDialogInput)

Export[SystemDialogInput["FileSave", NotebookDirectory[]], foo]

In notebooks that I use regularly, but with different input and output files, I like to have multiple FileNameSetter at the beginning of the notebook.
E.g.

Row[{FileNameSetter[Dynamic[fooFile], "Save"], 
     "   File, in which the results are saved   ", Dynamic[fooFile]}]

fooSetter

and than later in the program

Export[fooFile, foo]

Clicking on the Browse... button will bring up the Windows typical Save as dialog window and allows you to specify a filename, that does not jet exist in the usual way. The file itself will be created, when the Export function is evaluated.

$\endgroup$
  • $\begingroup$ Truly, you are wise, thanks! $\endgroup$ – Igor Rivin Sep 11 '14 at 0:11

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.