11

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"}
sakra
  • 5,120
  • 21
  • 33
  • 1
    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? – Sjoerd C. de Vries Jul 21 '15 at 19:08
  • Test if a file has been correctly installed on the front end machine. – sakra Jul 21 '15 at 19:11
  • But what use would that have for the running kernel? Are you perhaps comparing Mathematica installations on FrontEnd and remote PCs? – Sjoerd C. de Vries Jul 21 '15 at 19:12
  • 1
    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. – MarcoB Jul 21 '15 at 19:14
  • Upon manipulating front end options with SetOptions[$FrontEnd (e.g., NotebookBrowseDirectory), the kernel can make sure that a valid value is set. – sakra Jul 21 '15 at 19:22
  • Can't you use CurrentValue[$FrontEnd, NotebookAutoSave] for that? – Sjoerd C. de Vries Jul 21 '15 at 19:33
  • I guess that worked? Cool. :-) – Mr.Wizard Jul 21 '15 at 20:09

1 Answers1

9

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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • It works. The "MLFSFileType" is another good option. – sakra Jul 21 '15 at 20:10
  • What MLFS is except of the fact that it introduces some functions that may be run by FE? – Kuba Mar 03 '16 at 09:31
  • @Kuba I don't know. Maybe MathLink File System? +shrug+ – Mr.Wizard Mar 03 '16 at 09:50
  • @Mr.Wizard Makes sense ;) – Kuba Mar 03 '16 at 09:57
  • @Kuba BTW did you come here looking for a solution to (108640)? – Mr.Wizard Mar 03 '16 at 10:18
  • @Mr.Wizard No, I have to have things done so I'm using some specific workarounds and left that question open. I came here because I was looking for FE equivalent for FindFile. I know I can do FileNames[fileName, ToFileName/@AbsoluteCurrentValue[$FrontEnd, "StyleSheetPath"]] but it isn't the fastest solution, fast enough to use though, I was just curious. – Kuba Mar 03 '16 at 10:25
  • @Kuba if you haven't found it yet I updated the front-end packet types to include FrontEnd`FindFileOnPath which is what you want. – b3m2a1 Aug 18 '17 at 03:31