Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    How can I verify that a file exists

    Scheduled Pinned Locked Moved
    Using Duet Controllers
    5
    13
    694
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Aitorundefined
      Aitor
      last edited by

      How can I verify that a file exists

      I'm trying something like this but it doesn't work

      if { exists(0:/sys/config.g) }
      	M291 R "EXISTS?" P "YES"
      else
      	M291 R "EXISTS?" P "NO"
      

      How can I do it?

      Best regards and thanks

      dc42undefined 1 Reply Last reply Reply Quote 0
      • dc42undefined
        dc42 administrators @Aitor
        last edited by

        @aitor testing whether a file exists is not currently supported. What is your use case?

        Duet WiFi hardware designer and firmware engineer
        Please do not ask me for Duet support via PM or email, use the forum
        http://www.escher3d.com, https://miscsolutions.wordpress.com

        Aitorundefined 1 Reply Last reply Reply Quote 0
        • Aitorundefined
          Aitor @dc42
          last edited by

          Good morning @dc42,

          I want to check if the resurrect.g file exists when starting the machine, to be able to set the temperature of the base, because sometimes I have not been able to recover the piece because it had come unstuck.

          And I'm sure I can think of more applications later

          Thanks

          OwenDundefined 1 Reply Last reply Reply Quote 0
          • OwenDundefined
            OwenD @Aitor
            last edited by

            @aitor
            You can fudge it with M38 "filename"
            It will put an error message in the console if the file doesn't exist, but outside that it's workable.

            M38 "some file"
            if result != 0
            	M291 P"File doesn't exist!" R"Houston we have a problem" S0 T3
            else
            	M291 P"File exists."  R"Life is good" S0 T3
            
            
            Aitorundefined 1 Reply Last reply Reply Quote 0
            • Aitorundefined
              Aitor @OwenD
              last edited by

              Thank you very much @owend for your input

              It works fine, this is what I added in my config.g (is it the right place? where should I add it?)

              M38 "0:/sys/resurrect.g"
              if result != 0
              	M291 P "No file" R "resurrect.g does not exist" S0 T3
              else
              	M916
              

              But I would like to add one "if" more to avoid some problem that I have encountered, there is an object model that is "Last reset cause: power up" I can't find it if it exists.

              On the other hand, to achieve the same thing without getting an error, there would be a way to write in a file like this:

              M28 "0:/sys/runonce.g"
              M916
              M29
              

              I have tried adding it to the M911 command in these two ways:

              M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28 "0:/sys/runonce.g" M916 M29"
              

              Adding the quotes to M28 closes M911 and does not work.

              M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28 0:/sys/runonce.g M916 M29"
              

              Removing the quotes works but it takes it as a name (runonce.g M916 M29) and does not write anything inside.

              Do you know any way to avoid this?

              Best regards and thanks again

              OwenDundefined dc42undefined 2 Replies Last reply Reply Quote 0
              • OwenDundefined
                OwenD @Aitor
                last edited by OwenD

                @aitor
                To be honest I'm not sure if automated restarting after a failure is a smart move.
                There are lots of things that could go wrong if there's a power outage during a print.

                Also my code isn't quite accurate
                The result of the file doesn't exist would be 2
                M38 could fail for other reasons and return code 1
                So use

                iM38 "some file"
                if result == 2
                   echo "file doesn't exist
                

                Have you read this?
                https://duet3d.dozuki.com/Wiki/Setting_up_to_resume_a_print_after_a_power_failure

                Aitorundefined 1 Reply Last reply Reply Quote 0
                • Aitorundefined
                  Aitor @OwenD
                  last edited by

                  Good morning @owend,

                  You are right doing an automatic recovery is dangerous that's why my resurrect-prologue.g contains:

                  var temp = heat.heaters[1].active
                  
                  M116 ; wait for temperatures
                  G28 XY ; house X and Y, I hope Z hasn't moved
                  G10 P0 S0
                  M291 R "Recovery" P "Want to recover the part" S2 T0
                  G10 P0 S{var.temp}
                  M116 P0
                  

                  so I just keep the bed heater on until I get there.

                  if I recovery the part and ends, resurrect.g it will be deleted.

                  if it turns out that I don't want to recovery the part, I have added to cancel.g the following:

                  if global.delete = 1
                  	M471 S"/sys/resurrect.g" T{"/sys/resurrect1.g"} D1
                  	set global.delete = 0
                  

                  this is what I've added in my config.g (then I'll try the last thing you indicated)

                  global delete = 0
                  M38 "0:/sys/resurrect.g"
                  if result != 0
                  	don't do anything
                  else
                  	set global.delete = 1
                  	M916
                  

                  I really wanted to use the M291 command in my resurrect-prologue.g as S3, "ok " to continue and "cancel" to not continue, but I have not been able to, so I have had to do this less nice alternative for now.

                  Thanks for the help, it is being very useful and I am learning a lot.

                  oozeBotundefined 1 Reply Last reply Reply Quote 0
                  • dc42undefined
                    dc42 administrators @Aitor
                    last edited by

                    @aitor said in How can I verify that a file exists:

                    Adding the quotes to M28 closes M911 and does not work.

                    Use two double-quote characters inside a string literal:

                    M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28 ""0:/sys/runonce.g"" M916 M29"

                    Duet WiFi hardware designer and firmware engineer
                    Please do not ask me for Duet support via PM or email, use the forum
                    http://www.escher3d.com, https://miscsolutions.wordpress.com

                    Aitorundefined 1 Reply Last reply Reply Quote 0
                    • oozeBotundefined
                      oozeBot @Aitor
                      last edited by

                      @aitor said in How can I verify that a file exists:

                      I really wanted to use the M291 command in my resurrect-prologue.g as S3, "ok " to continue and "cancel" to not continue, but I have not been able to, so I have had to do this less nice alternative for now.

                      Maybe this will help?

                      https://forum.duet3d.com/topic/23423/m291-solution-to-handling-both-ok-cancel

                      1 Reply Last reply Reply Quote 0
                      • Aitorundefined
                        Aitor @dc42
                        last edited by

                        Good morning @dc42

                        This did not work:

                        M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28" "0: /sys/runonce.g" "M916 M29"
                        Error: M911: expected string expression

                        Good morning @oozebot

                        I've been trying it 😁 , very useful, but it didn't work for me or I didn't know how to make it work in my application, because I can exit the macro where I have the message (i.e. resurrect-prolonge.g) but I'm still in resurrect.g so no matter what I answer it keeps going.

                        For other applications it is a marvel that I am already implementing.

                        It would be nice to have a M291 S4 YES or NO that would assign TRUE or FALSE to an already defined global variable or something like that

                        Thanks again to all

                        Phaedruxundefined 1 Reply Last reply Reply Quote 0
                        • Phaedruxundefined
                          Phaedrux Moderator @Aitor
                          last edited by

                          @aitor said in How can I verify that a file exists:

                          This did not work:

                          Try without the spaces between the quotes.

                          Z-Bot CoreXY Build | Thingiverse Profile

                          1 Reply Last reply Reply Quote 0
                          • dc42undefined
                            dc42 administrators
                            last edited by

                            @aitor said in How can I verify that a file exists:

                            This did not work:
                            M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28" "0: /sys/runonce.g" "M916 M29"
                            Error: M911: expected string expression

                            Do not put space characters between the two double quote characters.

                            Duet WiFi hardware designer and firmware engineer
                            Please do not ask me for Duet support via PM or email, use the forum
                            http://www.escher3d.com, https://miscsolutions.wordpress.com

                            Aitorundefined 1 Reply Last reply Reply Quote 0
                            • Aitorundefined
                              Aitor @dc42
                              last edited by Aitor

                              Good morning @phaedrux @dc42 @owend @oozebot

                              When I deleted the double quotes it didn't give me an error, but it generated a blank file, I realised it was because I was trying to run M916 but it didn't exist yet, so I solved it this way.

                              M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28""0:/sys/runonce.g"" M98 P""run1.g"" M29"

                              in run1.g I typed M916 and this did work as expected

                              only that runonce.g is not deleted, something that is good for me because I thought that if I had two blackouts in a row without having recovered this would be deleted and would not work as I wanted, when I finish printing is not deleted either, only to restart again, this makes some errors appear, so I added to my final script and my cancel.g a M30 to delete it.

                              This was my solution and it works fine so far:

                              config.g I added:

                              M911 S22 R23 P "M913 X0 Y0 Z0 E0 M28""0:/sys/runonce.g"" M98 P""run1.g"" M29"
                              ; Global Variables
                              global delete = 0
                              

                              run1.g contains:

                              set global.delete = 1
                              M916
                              

                              resurrect-prologue.g contains:

                              var temp = heat.heaters[1].active
                              
                              M116 ; wait for temperatures
                              G28 XY ; house X and Y, hope Z hasn't moved
                              G10 P0 S0
                              M291 R "Recovery" P "Want to recover the part" S2 T0
                              G10 P0 S{var.temp}
                              M116 P0
                              

                              final script I added: (resurrect.g if automatically deleted)

                              if global.delete = 1
                              	M30 "/sys/runonce.g"
                              	set global.delete = 0
                              

                              cancel.g I added:

                              if global.delete = 1
                              	M471 S"/sys/resurrect.g" T{"/sys/resurrect1.g"} D1 ; I rename it in case I want to use it later on.
                              	M30 "/sys/runonce.g"
                              	set global.delete = 0
                              

                              I hope you find this solution useful and if you think of any improvement I will be happy to hear it.

                              Best regards and thanks for your support.

                              1 Reply Last reply Reply Quote 1
                              • First post
                                Last post
                              Unless otherwise noted, all forum content is licensed under CC-BY-SA