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

    File.writable? false for DOCUMENTS folder

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 5 Posters 415 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.
    • P Offline
      pgarmyn
      last edited by

      Ruby ALLWAYS reports "not writable" for the documents folder on my system, Windows 8.1.
      Even if i am administrator. And even if i can write in the directory.
      I suppose it is for security reasons (?)

      
      puts ENV["HOME"]  
      #output ; # C;/Users/USERNAME
      File.writable? ENV["HOME"]
      #output ; # true
      File.exist? ENV["HOME"]+"/Documents"
      #output ; # true
      File.writable? ENV["HOME"]+"/Documents"
      #output ; # false  ## THIS IS NOT LOGIC because i can write ; 
      File.exist? ENV["HOME"]+"/Documents/test"
      #output ; # false
      Dir.mkdir ENV["HOME"]+"/Documents/test"
      #output ; # 0
      File.exist? ENV["HOME"]+"/Documents/test"
      #output ; # true
      File.writable? ENV["HOME"]+"/Documents/test"
      #output ; # true
      
      

      It seems that plugins must ignore this false information ?
      Can someone confirm ?

      The properties of my documents folder are.
      Documents-prop.jpg
      Documents-prop1.jpg

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by

        what does it return if you add an additional /

        puts File.writable? ENV["HOME"]+"/Documents/"
        

        both variants return true on a mac...

        learn from the mistakes of others, you may not live long enough to make them all yourself...

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

          As you have discovered... File.writable?{file_or_folder_path) only works consistently/effectively on MACs.
          On PCs you can add a final ' /' and it might [sometimes] work... but I recommend that you really need to try to briefly write a temporary file to that folder in your own 'trapped' method, to get a consistent and 'correct' result... e.g.

          def self.writable?(directory_path=nil)
            return nil unless d=directory_path
            return nil unless File.directory?(d)
            begin
              r=rand.to_s
              r.next! while File.exist?(File.join(d, r)) #unique temp file trap
              t=File.join(d, r)
              f=File.new(t, 'w')
              f.puts("0") #it's made, can we modify it ?
              f.close
              if File.exist?(t)
                begin
                  File.delete(t)
                  w=true
                rescue #can write it BUT not change it !?
                  w=false
                end
              else
                w=false
              end
            rescue #can't write it at all !
              w=false
            end
            return w
          end
          

          This test is used thus self.writable?(some_directory_path), which returns true or false if a temporary file can be written to the folder, or it returns nil if no 'directory_path' was passed or the folder doesn't exist and/or it is not a directory !]...

          TIG

          1 Reply Last reply Reply Quote 0
          • S Offline
            slbaumgartner
            last edited by

            @pgarmyn said:

            @driven said:

            what does it return if you add an additional /

            puts File.writable? ENV["HOME"]+"/Documents/"
            

            both variants return true on a mac...

            Both false on my Win 8.1

            My experience is that 'Documents' is the exception on Windows.
            For other folders, the value is consistenly.

            Could this be related to the "shims" that recent versions of Windows use to fake some folders?

            1 Reply Last reply Reply Quote 0
            • P Offline
              pgarmyn
              last edited by

              @driven said:

              what does it return if you add an additional /

              puts File.writable? ENV["HOME"]+"/Documents/"
              

              both variants return true on a mac...

              Both false on my Win 8.1

              My experience is that 'Documents' is the exception on Windows 8.1.
              (I don't have non ASCII cars in the path)

              For other folders, the value is consistenly.

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

                Ruby's file handling under Windows is frustratingly flaky. We have reported a number of issues to the Ruby developers and I'd encourage people to file whatever you find.

                I usually wrap file handling in a begin, rescue block instead of testing if a while is writable - simply because Windows file handling is unpredictable.

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

                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