Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Delete files in a directory

Reply
Thread Tools

Delete files in a directory

 
 
thehcdreamer@gmail.com
Guest
Posts: n/a
 
      11-03-2007
Hello, this is my first post here, so first i would like to thanks all
the people who works for this site.

Well, i'm trying to set up a little script that should delete all the
files in a given drectory; the system should work like this:
-I have the name and the path of file = "/Users.....file1"
-from file, i should get the directory (I've used File.dirname(file))
-from that directory, i should delete all files and sub directories
except from the given file (in this case, file1)

I'm making confusion when i try to iterare on the files and irectory in
that folder, and i'm scared to make test of deleting files in the system

Oscar
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
stefano.crocco@alice.it
Guest
Posts: n/a
 
      11-03-2007
Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Alle sabato 3 novembre 2007, Oscar Del ben ha scritto:
> Hello, this is my first post here, so first i would like to thanks all
> the people who works for this site.
>
> Well, i'm trying to set up a little script that should delete all the
> files in a given drectory; the system should work like this:
> -I have the name and the path of file = "/Users.....file1"
> -from file, i should get the directory (I've used File.dirname(file))
> -from that directory, i should delete all files and sub directories
> except from the given file (in this case, file1)
>
> I'm making confusion when i try to iterare on the files and irectory in
> that folder, and i'm scared to make test of deleting files in the system
>
> Oscar


If I understand correctly what you need to do, this should work.

require 'fileutils'

file_name = File.basename(file_path)
dir = File.dirname(file_path)
Dir.foreach(dir) do |f|
if f == file_name or f == '.' or f == '..' then next
elsif File.directory?(f) then FileUtils.rm_rf(f)
else FileUtils.rm( f )
end
end

To test the script, you can replace FileUtils.rm_rf(f) and FileUtils.rm(f) respectively with

FileUtils.rm_rf( f, :noop => true, :verbose => true)

and

FileUtils.rm( f, :noop => true, :verbose => true)

With those arguments, FileUtils.rm and FileUtils.rm_rf don't actually delete the files (or directories), but print the operation that would be performed on screen, so you can check whether everything works. For more information on this, look at the ri documentation for FileUtils and FileUtils.rm.

I hope this helps

Stefano

 
Reply With Quote
 
 
 
 
thehcdreamer@gmail.com
Guest
Posts: n/a
 
      11-04-2007

Thank you Stefano, it work good for me!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
andrea.fazzi@alca.le.it
Guest
Posts: n/a
 
      11-05-2007
require 'fileutils'

def delete(filename)
Dir["#{File.dirname(filename)}/*"].each do |file|
next if File.basename(file) == File.basename(filename)
FileUtils.rm_rf file, :noop => true, :verbose => true
end
end

USAGE:

delete 'path/to/file1'

Bye.
Andrea

Il giorno sab, 03/11/2007 alle 19.39 +0900, Oscar Del ben ha scritto:
> Hello, this is my first post here, so first i would like to thanks all
> the people who works for this site.
>
> Well, i'm trying to set up a little script that should delete all the
> files in a given drectory; the system should work like this:
> -I have the name and the path of file = "/Users.....file1"
> -from file, i should get the directory (I've used File.dirname(file))
> -from that directory, i should delete all files and sub directories
> except from the given file (in this case, file1)
>
> I'm making confusion when i try to iterare on the files and irectory in
> that folder, and i'm scared to make test of deleting files in the system
>
> Oscar



 
Reply With Quote
 
thehcdreamer@gmail.com
Guest
Posts: n/a
 
      11-05-2007
Thank you!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.Delete Throws "the directory is not empty" codeboy ASP .Net 1 06-08-2009 12:08 PM
Recursive delete of directory - Directory not empty error Gordon Javascript 1 04-14-2008 06:28 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Java Program delete files in directory also include subdirectory moon Java 3 09-20-2005 01:31 PM
Explorer takes minutes to view directory contents, delete files Yaroslav Bulatov Computer Support 3 01-01-2004 06:40 PM



Advertisments