File exists check
-
It would be good if we could get the result of whether a file exists in G Code.
For example could M36 be changed to return an error code 1 or 2 if the file isn't found?
At present it returns the error state as part of the JSON response but returns and error state of zero even if the file isn't found. -
I've added this to the work list.
-
@dc42 Has it been decided what firmware version this will be in? Thanks!
-
@donstauffer I believe it will be in 3.3
exists name -> bool Yields true if 'name' is a valid variable or object model element name and is not null (available in RRF 3.3beta3 and later). Especially useful for testing whether a particular parameter has been provided when a file macro was called.
https://duet3d.dozuki.com/Wiki/GCode_Meta_Commands#Section_Functions
-
@phaedrux This is about files, not variables.
-
@donstauffer ah, my mistake.
-
@phaedrux I found a very awkward workaround: M471 is the only code I know of which returns a result that tells you something about the existence of a file. Unfortunately, it does that when potentially deleting the file in question. I didn't want that, so I had to set up a system of dummy "semaphor" files created in parallel with the file I was interested in, which fortunately my macros created, so it could keep the semaphors consistent with the actual file status.
But all that turned what would have been about a half page of code with a file exists capability into about 2 pages.
-
@dc42 would have to comment on whether it's been implemented yet for sure.
-
As a work around you can use M38.
M38: Compute SHA1 hash of target file
Example:
M38 gcodes/myfile.g
Used to compute a hash of a file on the SD card and returns a hexadecimal string which is the SHA1 of the file. If the file cannot be found, then the string "Cannot find file" is returned instead.
-
@owend How can my GCode use the string returned by M38? For example, how could I use it in a conditional? How do I suppress the spurious error message which displays in the console and as a pop-up in the web interface?
-
@donstauffer
You can't suppress the error message.
You don't exactly use the string. It's more a case of checking the result.
A result code of zero means I it's found and RRF was able to create a hash.
A result of 2 means the files wasn't found.
A result of 1 means the call to M38 returned a warning but I'm not sure of what conditions might return that result.It's not very elegant but it's the best we have for now.
M38 "gcodes/my file.g" if result = 2 echo "file not found" elif result = 1 echo "M38 call failed" elif result = 0 echo "file exists"
-
@dc42 Is there any news about this. It would be really nice to check if a file exists or not.
-
@mher a new function fileexists(name) is already implemented in the RRF 3.5 source code.