From: Robert Klemme Date: 2010-10-05T00:02:03+09:00 Subject: Re: how do i delete files in particular directoryin ruby ??? On Mon, Oct 4, 2010 at 2:27 PM, Stefano Crocco wrote: > On Monday 04 October 2010, Amit Tomar wrote: >> |Hii all, >> |         i would like to know ,how do i delete all files in particular >> |directoy .i don't know about name of files but i know from which >> |directory i have to delete files.how should i do that?? > > The ri documentation for FileUtils.rm gives several examples, among which you > can find this: > > FileUtils.rm Dir.glob('*.so') > > This removes all files with extension .so from the current directory. To > remove *all* files from the current directory, you need to replace '*.so' with > '*' (which matches all file names): > > require 'fileutils' > FileUtils.rm Dir.glob('*') > > If you want to remove all files from another directory, you do: > > require 'fileutils' > FileUtils.rm Dir.glob('/path/to/dir/*') > > Note that this will give you errors in case the directory also contains > subdirectories. In this case, the correct approach depends on what you want to > obtain > * if you also want to remove the subdirectories, replace FileUtils.rm with > FileUtils.rm_r, which also remove directories > * if you don't want to touch subdirectories and their content, you can do > something like this: > > Dir.glob('*').each do |f| >  FileUtils.rm f unless File.directory? f > end > > * if you want to delete the files in the subdirectories but keep the empty > subdirectories, you can do this: > > require 'find' > Find.find('.') do |f| >  next if File.directory? f >  FileUtils.rm f > end > > I hope this helps And then there is also FileUtils.rm_r and FileUtils.rm_rf. :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/