9
$\begingroup$

Export["filepath1",Expression1] works for me, but if filepath1 already exists, it will be overwritten. I'd like Export to notify me and ask for a confirmation, if it tries to overwrite filepath1.

I was thinking of creating a child function Export1 that would:

  1. check if the target file exists
  2. when 1. obtains, ask if I'd like to overwrite
  3. when 2. obtains and I confirm, pass all the parameters and attributes to the parent function Export

So, I'd like to create a "child" function that would have exactly the same syntax as the "parent" one, but would be doing an extra check.

What is the best way to do it?

P.S.

Export doesn't have attributes (like, say, Plot has PlotRange -> All), but I'd like to learn how to create a modified child function that would accept and pass not only the parameters, but also attributes to the parent one.

$\endgroup$
  • $\begingroup$ user, please see my updated answer. I realized that the normal OptionsPattern method recommended in the link is not applicable to Export. Sorry for any confusion this may have caused. $\endgroup$ – Mr.Wizard Feb 24 '15 at 18:45
14
$\begingroup$

Your basic requirement is met with:

safeExport[file_String, args___] :=
  If[
    ! FileExistsQ[file] || ChoiceDialog["File already exists. Overwrite?"], 
    Export[file, args],
    $Failed
  ]

What you describe as "attributes" (e.g. PlotRange -> All) are known as Options or named optional arguments. (See Attributes for a description of what that term means in Mathematica.) To learn how to set up Options please see:

My answer to that question specifically deals with a custom function that is an extension of a built-in, with additional Options.


Regarding the Options aspect of my answer I realize there is a problem when trying to apply the method described in my linked answer. This is because Export has no "official" Options (Option[Export] returns {}) despite taking a variety of special options for its different formats. Therefore if one follows my guide and uses a pattern like OptionsPattern[{Export, export2}] in the definition of a function export2 you will get error messages like:

export2["foo.gif", {1, 2, 3}, "DitheringMethod" -> None]

OptionValue::nodef: Unknown option DitheringMethod for {Export,export2}. >>

Essentially Export breaks the new Options convention since it does not have rigorously defined Options. One could use the deprecated method OptionQ described by Leonid here, but I think if Option-like behavior is needed it would be better to use a different workaround. I propose:

Options[export2] = {"Confirmation" -> True};

export2[OptionsPattern[]] := If[OptionValue["Confirmation"], safeExport, Export]

Usage examples:

export2[]["foo.gif", {1, 2, 3}, "DitheringMethod" -> None]

SetOptions[export2, "Confirmation" -> False];
export2[]["foo.gif", {1, 2, 3}, "DitheringMethod" -> None]

export2["Confirmation" -> True]["foo.gif", {1, 2, 3}, "DitheringMethod" -> None]
$\endgroup$

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.