• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

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.
  • T Offline
    TIG Moderator
    last edited by 4 Sept 2016, 12:33

    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
    • I Offline
      Iltis
      last edited by 4 Sept 2016, 12:44

      OK, will try it. Thank you!

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 4 Sept 2016, 13:24

        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 4 Sept 2016, 15:09

          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
          • I Offline
            Iltis
            last edited by 5 Sept 2016, 06:57

            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 5 Sept 2016, 11:01

              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
              • T Offline
                thomthom
                last edited by 5 Sept 2016, 11:03

                @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
                • T Offline
                  thomthom
                  last edited by 5 Sept 2016, 11:04

                  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 5 Sept 2016, 11:18

                    @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
                    • I Offline
                      Iltis
                      last edited by 5 Sept 2016, 11:25

                      Thank you very much for these complementary solutions.

                      Renaud.

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

                      Advertisement