11
$\begingroup$

When using a remote Mathematica kernel, how can you test if a file exists on the local front end machine? Of course FileExistsQ cannot be used in this scenario, because it only works on the kernel file system.

There are some undocumented functions in the FrontEnd context, which may do the trick with FrontEndExecute:

Names["FrontEnd`*File*"]

yields

{"FrontEnd`FileBrowse", "FrontEnd`Filename", "FrontEnd`FileName", "FrontEnd`FindFileOnPath", "FrontEnd`ToFileName"}
$\endgroup$
  • 1
    $\begingroup$ Just curious: If you can't open a file using a kernel running on a remote machine what use does it have to know whether a file exists on a local machine? $\endgroup$ – Sjoerd C. de Vries Jul 21 '15 at 19:08
  • $\begingroup$ Test if a file has been correctly installed on the front end machine. $\endgroup$ – sakra Jul 21 '15 at 19:11
  • $\begingroup$ But what use would that have for the running kernel? Are you perhaps comparing Mathematica installations on FrontEnd and remote PCs? $\endgroup$ – Sjoerd C. de Vries Jul 21 '15 at 19:12
  • 1
    $\begingroup$ I agree with @Sjoerd that maybe we are missing some details of your problem here. Generally speaking, it seems to me that the remote kernel must have access to your local file to do the check. Importing and exporting results from remote kernels suffer from similar problems, and some questions about them have cropped up before (see (18016) and (19394)), with limited success, and always requiring the remote kernel to have access to the local system. $\endgroup$ – MarcoB Jul 21 '15 at 19:14
  • $\begingroup$ Upon manipulating front end options with SetOptions[$FrontEnd (e.g., NotebookBrowseDirectory), the kernel can make sure that a valid value is set. $\endgroup$ – sakra Jul 21 '15 at 19:22
9
$\begingroup$

I don't have any kernels running on a remote machine so I cannot be sure of how this is handled, but I think it has as good a chance as any of working.

A file that exists:

MathLink`CallFrontEnd[MLFS`FileByteCount["A:\\foo.txt"]]
16

A file that does not exist:

MathLink`CallFrontEnd[MLFS`FileByteCount["A:\\bar.txt"]]
$Failed

Other commands in the same context that may prove useful:

Names["MLFS`*"]
{"MLFS`Close", "MLFS`CopyDirectory", "MLFS`CopyFile", "MLFS`CreateDirectory", 
"MLFS`DeleteDirectory", "MLFS`DeleteFile", "MLFS`FileByteCount", "MLFS`FileDate", 
"MLFS`FileNames", "MLFS`FileType", "MLFS`Get", "MLFS`OpenAppend", "MLFS`OpenRead", 
"MLFS`OpenWrite", "MLFS`Put", "MLFS`PutAppend", "MLFS`Read", "MLFS`RenameDirectory", 
"MLFS`RenameFile", "MLFS`SetFileDate", "MLFS`SetStreamPosition", "MLFS`StreamPosition", 
"MLFS`WriteString"}

Found via:

$\endgroup$
  • $\begingroup$ It works. The "MLFSFileType" is another good option. $\endgroup$ – sakra Jul 21 '15 at 20:10
  • $\begingroup$ What MLFS is except of the fact that it introduces some functions that may be run by FE? $\endgroup$ – Kuba Mar 3 '16 at 9:31
  • $\begingroup$ @Kuba I don't know. Maybe MathLink File System? +shrug+ $\endgroup$ – Mr.Wizard Mar 3 '16 at 9:50
  • $\begingroup$ @Mr.Wizard Makes sense ;) $\endgroup$ – Kuba Mar 3 '16 at 9:57
  • $\begingroup$ @Kuba BTW did you come here looking for a solution to (108640)? $\endgroup$ – Mr.Wizard Mar 3 '16 at 10:18

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.