From: Robert Klemme Date: 2010-10-07T18:11:40+09:00 Subject: Re: how do i close a file that is open in ruby On Thu, Oct 7, 2010 at 9:34 AM, Amit Tomar wrote: > Robert Klemme wrote: >> On Thu, Oct 7, 2010 at 8:59 AM, Amit Tomar >> wrote: >>> Hi all, >>> ? ? ? ? i would like to close a file that is open in particular >>> directory .this is my code >>> >>> require 'fileutils' >>> >>> ? ?dir ="C:/Documents and Settings/x0138466/Local Settings/Temp" >>> ? ?Dir.chdir(dir) >>> ? ?Dir["*"].each do |f| >>> ? ?f.close >> >> f is a String!  You cannot close a String. >> >>> ? ? ? ?# ?File.close >>> >>> ? ? ?FileUtils.rm_rf(f) >>> ? ? #FileUtils.rm(f) >>> >>> ?end >>> ? ?Some of the file in TEMP folder is open or you can say used by some >>> process but i want to close but with above code am not ablr to do >>> that??is it possible to achive from above code?? >> >> From your recent postings I conclude that you mean "opened by another >> process".  No, you cannot close a file from another process without >> going through major operating system specific tricks (on Windows there >> is ProcessExplorer of former Sysinternals for example).  You also >> usually need specific privileges, i.e. have root rights on a *nix >> system and have administrative permissions on a Windows box. >> >> Kind regards >> >> robert > > Thanks for your response robert > i downloaded  a tool handle using which i am able to close the file that > is in use but i don't know how do i acheve this form ruby code am > running this command from command prompt to close the file > > handle -p 556 -c 750 You can easily do the same with system(). > ruby.exe pid: 556 TIGOC\x0138466 >  748: File  (RW-)   C:\Documents and Settings\x0138466\Local > Settings\Temp >  760: File  (RW-) > C:\InstantRails-2.0-\rails_apps\upload\log\development.log >  784: File  (RW-) > C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-00.2982_x-ww_ac3f9c03 >  7B0: File  (RW-) > C:\InstantRails-2.0-\rails_apps\upload\log\development.log >  750: File  (RW-)   C:\DOCUME~1\x0138466\LOCALS~1\Temp\mongrel.556.1 > where 750 is the handle i closed However, I'd be extra careful to close file handles behind the back of the process that uses it. Usually code does not handle this very well. Also, you probably better resolve the root cause that lead to the handle being open longer than you intended. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/