sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    How detect that a file is locked by another app?

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 5 Posters 991 Views 5 Watching
    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.
    • IltisI Offline
      Iltis
      last edited by

      Hello driven,

      Thank you for your answer, but I think it's not what I need.
      In my case, in the real code (not my example here), the export is in a .dxf file. To test the quality/errors of the export, the .dxf file is open in a CAD software. Then, if I try in SU to overwrite the file with my plugin (after some modification in the SU model for example), and if the file is already opened in the CAD software, the file is locked by the CAD software. If I make this with a .txt file opened in Notepad++, there is no problem, because Notepad is not locking the file (and will detect the change!).
      I want to track the error and say to the user to close the file in the CAD software or to rename the file.

      (I'm french, sorry for my writting mistakes.)
      Renaud.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        out_name = UI.savepanel('Location', '' , "#{File.basename(model.path, ".*")}.txt")
        begin
          if out_name && ! out_name.empty?
            if File.extname(out_name).downcase != ".txt"
              ### if the user erase the extension, we readd it!
              out_name += ".txt"
            end
            file = File.new(out_name, 'w')
            file.puts(%Q["blabla"])
            file.close()
          else
            puts "Canceled"
          end
        rescue Exception => error
          puts error
          ### or do something else like
          UI.messagebox("#{error}\nFailed to export file #{file}")
        end
        

        TIG

        1 Reply Last reply Reply Quote 0
        • IltisI Offline
          Iltis
          last edited by

          OK, will try it. Thank you!

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Just noticed a typo!
            I corrected the earlier code snippet...

            if out_name &**&** ! out_name.empty?

            One other thing to note is that some types of file are locked by the app that opens them - like a dxf.
            But a txt file is opened by Notepad++.exe it is not locked, so its contents can be overwritten, you will then be asked when bringing that txt file's window foremost in Notepad++ if you want to reload it...

            So try using the dxf file...
            Or for a simple test write a .CSV file [remembering to force it to .csv, rather than .txt] AND have it already opened in Excel - which will lock it...

            TIG

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              Did you try something like File.writable?("file")?

              Link Preview Image
              Class: File (Ruby 2.0.0)

              Class : File - Ruby 2.0.0

              favicon

              (ruby-doc.org)

              Hi

              1 Reply Last reply Reply Quote 0
              • IltisI Offline
                Iltis
                last edited by

                TIG, your solution is working fine. Thank you very much. 👍

                @jim said:

                Did you try something like File.writable?("file")?

                Yes Jim, not working. 😞

                BR,
                Renaud.

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  In that case I would write it like this. You might rescue StandardError also, but rescuing Exception is too broad. You generally want to rescue exceptions starting with the most-specific and work up to the least-specific.

                  [1] https://www.google.com/search?q=ruby+rescue+exception%26amp;ie=utf-8%26amp;oe=utf-8

                  
                  def locked?(filename)
                     locked = false
                     begin
                        file = File.open(filename, "w")
                     rescue Errno;;EACCES => error
                        locked = true
                     ensure
                        file.close if file
                     end
                     return locked
                  end
                  
                  if $0 == __FILE__
                     p locked?("test.txt")
                  end
                  
                  
                  

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    @jim said:

                    In that case I would write it like this. You might rescue StandardError also, but rescuing Exception is too broad. You generally want to rescue exceptions starting with the most-specific and work up to the least-specific.

                    +1

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      Also, prefer opening files using block syntax so it always closes - saves you explicit rescue to close.

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • J Offline
                        Jim
                        last edited by

                        @unknownuser said:

                        Also, prefer opening files using block syntax so it always closes - saves you explicit rescue to close.

                        ...which actually simplifies everything.

                        
                        def write_to_file(filename)
                           File.open(filename, "a") { |file|
                              file.puts("Hello #{Time.now}")
                           }
                        rescue Errno;;EACCES
                           puts "Could not write to file."
                        end
                        
                        

                        Hi

                        1 Reply Last reply Reply Quote 0
                        • IltisI Offline
                          Iltis
                          last edited by

                          Thank you very much for these complementary solutions.

                          Renaud.

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement