how do i delete files in particular directoryin ruby ???

A

Amit Tomar

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

Stefano Crocco

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

Amit Tomar

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
 
S

Steel Steel

Amit said:
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) }
 
A

Amit Tomar

Steel said:
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:in
`remove_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:in
`remove_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:in
`rm'
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:in
`rm'
from ruby2.rb:8
from ruby2.rb:5:in `each'
from ruby2.rb:5
 
S

Stefano Crocco

|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

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
 
S

Stefano Crocco

|Steel Steel wrote:
|> Amit Tomar 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:in
|`remove_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:in
|`remove_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:in
|`rm'
| 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: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
 
A

Amit Tomar

Stefano said:
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??????
 
A

Amit Tomar

AND the files am looking to delete is ruby's Temp files (Cgi files)...
is there any way to delete them??
 
S

Stefano Crocco

|Stefano Crocco wrote:
|> On Monday 04 October 2010, Amit Tomar 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??????

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
 
A

Amit Tomar

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

Robert Klemme

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 wa= nt to
obtain
* if you also want to remove the subdirectories, replace FileUtils.rm wit= h
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|
=A0FileUtils.rm f unless File.directory? f
end

* if you want to delete the files in the subdirectories but keep the empt= y
subdirectories, you can do this:

require 'find'
Find.find('.') do |f|
=A0next if File.directory? f
=A0FileUtils.rm f
end

I hope this helps

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

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Jeremy Bopp

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
 
A

Amit Tomar

Jeremy said:
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??
 
J

Jeremy Bopp

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

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
 
A

Amit Tomar

Jeremy said:
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
 
J

Jeremy Bopp

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

Unfortunately, either Mongrel or your application is still using those
files; otherwise, you would be able to delete the files on Windows.
Even on non-Windows then, deleting the files won't reclaim the space
until after the program with the open file handles stops and releases
the files, so if consumption of disk space is your concern, you need to
find a way to close those files even if you manage to delete them.

I'm not familiar enough with Mongrel to give anything more than general
suggestions, so take this for what it's worth. Confirm that your
application is not causing these files to be opened and never closed by
way of a long-lived File instance referenced in one of your objects.
Maybe you can boil down your application to just this upload feature and
use that as a simple test case to prove that Mongrel is the one holding
onto these files. If the temporary file really is created by Mongrel
itself, perhaps there is a configuration option to tell Mongrel to
periodically dump its temporary files and create new ones.

-Jeremy
 
A

Amit Tomar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top