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:
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
···
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??
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??
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
And then there is also FileUtils.rm_r and FileUtils.rm_rf.
Cheers
robert
···
On Mon, Oct 4, 2010 at 2:27 PM, Stefano Crocco <stefano.crocco@alice.it> 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:
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
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
···
On Monday 04 October 2010, Amit Tomar wrote:
>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
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
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
···
On 10/05/2010 04:11 AM, Amit Tomar wrote:
Robert Klemme wrote:
On Mon, Oct 4, 2010 at 2:27 PM, Stefano Crocco <stefano.crocco@alice.it> >> 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.
Cheers
Thanks Robert
but what is FileUtils.rm_r and FileUtils.rm_rf for??
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
···
On Monday 04 October 2010, Amit Tomar wrote:
>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
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??
--
Posted via http://www.ruby-forum.com/\.
>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???
--
Posted via http://www.ruby-forum.com/\.
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
···
On 10/05/2010 07:31 AM, Amit Tomar wrote:
Jeremy Bopp wrote:
On 10/05/2010 04:11 AM, Amit Tomar 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??
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 Monday 04 October 2010, Amit Tomar wrote:
>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???
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
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
···
On 10/5/2010 8:10 AM, Amit Tomar wrote:
Jeremy Bopp wrote:
On 10/05/2010 07:31 AM, Amit Tomar 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
Why? Are you running into file system limitations due to size and
number of files? Or are you just offended by temporary files on
principle, like I am?
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
Does the Mongrel process handling the upload balloon up as well as
create a file that cannot be reclaimed?
If so, create a minimal app (as has been suggested), and see if you
can narrow down the problem. It is possible that a) either your Ruby
program is not garbage collected, or that Mongrel shows a strange bug
when dealing with large downloads.
···
On Tue, Oct 5, 2010 at 3:10 PM, Amit Tomar <amittomer25@yahoo.com> wrote:
--
Phillip Gawlowski
Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.