How do i delete files in particular directoryin ruby?

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??

On Monday 04 October 2010, Amit T. 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

Stefano

Thanks for your help Stefano
but when i trying to delete files in particular direcotry , am getting
permission denied ,this is how am trying to delete files

require ‘fileutils’

dir =“C:/DOCUME~1/x0100039/LOCALS~1/Temp”
Dir.chdir(dir)
Dir.glob(’.’).each do|f|
#puts f

FileUtils.rm(f)
#f.unlink

f.delete(’*.tmp’)

end

am trying to change the permission also but didn’t work for me

Amit T. wrote:

dir =“C:/DOCUME~1/x0100039/LOCALS~1/Temp”

Try using the full path (without ~)

Steel S. wrote:

Amit T. wrote:

dir =“C:/DOCUME~1/x0100039/LOCALS~1/Temp”

Try using the full path (without ~)

Thanks steel i tried with full name but still getting problem

require ‘fileutils’

dir =“C:/Documents and Settings/x0100039/Local Settings/Temp”
Dir.chdir(dir)
Dir.glob(’*’).each do|f|

FileUtils.rm(f)

end
but am getting these error:
C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1297:in unlink': Permission denied - CGI.3408.1 (Errno::EACCES) from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1297:inremove_file’
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1305:in
platform_support' from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1296:inremove_file’
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:771:in
remove_file' from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:549:inrm’
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:548:in
each' from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:548:inrm’
from ruby2.rb:8
from ruby2.rb:5:in `each’
from ruby2.rb:5

On Monday 04 October 2010, Amit T. wrote:

|
| FileUtils.rm(f)
| #f.unlink
|# f.delete(’*.tmp’)
|
| end
|
|am trying to change the permission also but didn’t work for me

Your code seems correct. Since I don’t use windows, I can’t help you
much
there. Seeing your commented out puts makes clear you already checked
the file
names were correct (I’d also try with p rather than puts, to see the raw
string, just in case). Does the error happen for all files, or just for
some
of them? Also, when you say you tried changing permissions, did you try
only
on the files or also on the directory itself (provided that directories
can
have permissions on windows. It’s been some years since I last made
serious
use of windows, so I don’t remember)?

Stefano

Stefano this is how am trying to change permission

f.chmod(0777)

Amit T. 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??

Dir["*"].each {|f| File.delete(f) }

Stefano C. wrote:

On Monday 04 October 2010, Amit T. wrote:

|dir =“C:/Documents and Settings/x0100039/Local Settings/Temp”
| from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1297:in
|each' | from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:548:in |rm’
| from ruby2.rb:8
| from ruby2.rb:5:in `each’
| from ruby2.rb:5

Have you tried removing one of those files by hand? If that works, then
we now
the issue is with ruby. If it doesn’t, you may get a clearer error
message
and, at least, we’ll know the issue is not with your code.

Stefano

Stefano what you want to say is if we are not able to delete with hand
,we can cann’t do it with code???

AND the files am looking to delete is ruby’s Temp files (Cgi files)…
is there any way to delete them??

On Monday 04 October 2010, Amit T. wrote:

|dir =“C:/Documents and Settings/x0100039/Local Settings/Temp”
| from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:1297:in
|each' | from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/fileutils.rb:548:in |rm’
| from ruby2.rb:8
| from ruby2.rb:5:in `each’
| from ruby2.rb:5

Have you tried removing one of those files by hand? If that works, then
we now
the issue is with ruby. If it doesn’t, you may get a clearer error
message
and, at least, we’ll know the issue is not with your code.

Stefano

Thanks Stefano…
yaa when am trying to delete files manually am getting cann’t delete
file .
It is in use…

On Monday 04 October 2010, Amit T. wrote:

|>> |`rm’
|>
|> Stefano
|
|Stefano what you want to say is if we are not able to delete with hand
|,we can cann’t do it with code???

What I mean is that if you launch the file manager (clicking on the “My
Computer” icon, for example), navigate to the directory where your files
are,
select one of them, press the Delete button on your keyboard and you get
an
error message saying you can’t delete the file (hopefully with an
explaination
of why you can’t delete it) then, of course, you also won’t be able to
delete
it from ruby. If windows thinks a file can’t be deleted (or at least it
can be
deleted only from the system administrator, for example), then it won’t
allow
you to delete it, and it doesn’t matter whether you’re using the file
manager,
ruby or some other program to delete the file: they all have to use the
same
functionality provided by windows to do so.

Stefano

On Mon, Oct 4, 2010 at 2:27 PM, Stefano C. [email protected]
wrote:

FileUtils.rm Dir.glob(‘/path/to/dir/*’)
FileUtils.rm f unless File.directory? f

I hope this helps

And then there is also FileUtils.rm_r and FileUtils.rm_rf. :slight_smile:

Cheers

robert

Robert K. wrote:

On Mon, Oct 4, 2010 at 2:27 PM, Stefano C. [email protected]
wrote:

FileUtils.rm Dir.glob(‘/path/to/dir/*’)
?FileUtils.rm f unless File.directory? f

I hope this helps

And then there is also FileUtils.rm_r and FileUtils.rm_rf. :slight_smile:

Cheers

Thanks Robert
but what is FileUtils.rm_r and FileUtils.rm_rf for??

On 10/05/2010 04:11 AM, Amit T. wrote:

Cheers

Thanks Robert
but what is FileUtils.rm_r and FileUtils.rm_rf for??

FileUtils.rm emulates the function of the Unix rm program.
FileUtils.rm_r and FileUtils.rm_rf emulate that program when using the
-r and -rf options respectively. The -r option (or _r) tells rm to work
recursively, to delete any subdirectories and their contents as well as
files in the selected directory. The -rf option (or _rf) is also
recursive but it adds a force option that will allow rm to delete files
that you own but which have been marked read-only. Take special care
with that one!

-Jeremy

Jeremy B. wrote:

On 10/05/2010 04:11 AM, Amit T. wrote:

Cheers

Thanks Robert
but what is FileUtils.rm_r and FileUtils.rm_rf for??

FileUtils.rm emulates the function of the Unix rm program.
FileUtils.rm_r and FileUtils.rm_rf emulate that program when using the
-r and -rf options respectively. The -r option (or _r) tells rm to work
recursively, to delete any subdirectories and their contents as well as
files in the selected directory. The -rf option (or _rf) is also
recursive but it adds a force option that will allow rm to delete files
that you own but which have been marked read-only. Take special care
with that one!

-Jeremy

Thanks jeremy …
but am on windows side and can i delete those files also that are in use
means when am trying to manually delete particular file(mongrel temp
file)am getting error message can’t delete files its in use
but usinf -rf can i delete those files also??

On 10/05/2010 07:31 AM, Amit T. wrote:

FileUtils.rm_r and FileUtils.rm_rf emulate that program when using the
but am on windows side and can i delete those files also that are in use
means when am trying to manually delete particular file(mongrel temp
file)am getting error message can’t delete files its in use
but usinf -rf can i delete those files also??

You’re bumping into a common limitation of Windows, sadly. It takes
some special tricks to fake deleting a file which is in use on Windows,
and I’m not certain that FileUtils has been written to perform such
tricks. Note that I did say “fake deleting” since the file is not
really deleted until it is no longer in use. It’s just cleverly moved
and hidden from what I understand.

Cygwin and the Cygwin build of Ruby should be able to do this for you,
but you are doing something which is not normal for Windows. Why do you
want to delete this temporary file while Mongrel is running in the first
place?

-Jeremy

Jeremy B. wrote:

On 10/05/2010 07:31 AM, Amit T. wrote:

FileUtils.rm_r and FileUtils.rm_rf emulate that program when using the
but am on windows side and can i delete those files also that are in use
means when am trying to manually delete particular file(mongrel temp
file)am getting error message can’t delete files its in use
but usinf -rf can i delete those files also??

You’re bumping into a common limitation of Windows, sadly. It takes
some special tricks to fake deleting a file which is in use on Windows,
and I’m not certain that FileUtils has been written to perform such
tricks. Note that I did say “fake deleting” since the file is not
really deleted until it is no longer in use. It’s just cleverly moved
and hidden from what I understand.

Cygwin and the Cygwin build of Ruby should be able to do this for you,
but you are doing something which is not normal for Windows. Why do you
want to delete this temporary file while Mongrel is running in the first
place?

-Jeremy

Yaa Jeremy i really need do delete those file…

In my rails application i am uploading some large file (2gb) and while
doing mongrel +ruby temp file is being created ,ruby temp files is not
the problem because they are garbaselly collecting after upload but
mongerl temprary files are sitll holding memory
that is why iw ant to delete them only after i restart mongerl i get
ridoff those temp files but i don’t restart server

Thansk Jeremy for your suggestion
Actully i googled a lot but could not find any document where i find how
mongerl
handles upload,actully mongrel is doing garbase collection but not the
way i want or aplication want.this is actully what;s happening

mongrel1.1.2
ruby 1.8.6
apche 2.2.16
i am running Apche frontend to mongrel and trying to upload some large
file(from rails application) to filesystem.Few things i observed.

1 - The uploaded data is stored in a file “mongrel.4124.0”
2 - the mongrel file is copied to a CGI.4124.0 file (I suppose this is
the regular Ruby tempfile)
3 - the tempfile is copied to the right location with the code above.

after upload finishes ruby’s temporary file is being garbaselly
collected but mongrel temporary file is still holding memory and i would
like to avoid this situation.
Now again i tried to uplaod same files same things happens but this TIME
mongrel old temorary file is being deleted but new mongerl file again
holding the memory…
what
coud i do now
cud you please provide me some good link where i can find more
information about mongrel like how it handles upoload or it do garbase
collection