Removing files if those exists

Hi,

I just started to work with ruby and I do not know how to do this.

So I have files junk1.cs, junk2.cs, junk3.cv etc. in the same folder.
There is also other files in the same folder that cannot be deleted.

What I would like to do is

If file junk*.cs exist

  • remove all junk*.cs files from dir

Thanks in advance.

On Wed, Oct 20, 2010 at 1:54 AM, Simo M. [email protected] wrote:

  • remove all junk*.cs files from dir

Thanks in advance.


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

First select them using Dir[] (you can try to get fancy with a pattern
here,
but I don’t think there is a way to specify multiple digits, so
something
like junky.cs would get picked up by a “junk*.cs” and “junk[0-9].cs”
wouldn’t pick up junk11.cs, so I’d just use “*” for the pattern.
http://ruby-doc.org/core/classes/Dir.html#M002323

Then, use the grep method to select the ones you want, since we didn’t
use
the pattern above. You’ll have to give it a regexp, if you aren’t
familiar
with that, you can just use /\Afile\d+.cs\Z/ and change out the name
and
extension as necessary. It basically says if the string begins with
“file”,
followed by one or more digits, followed by “.cs” and nothing else, then
it
is a match.
http://ruby-doc.org/core/classes/Enumerable.html#M003121

Then use each to iterate over the list of file names
http://ruby-doc.org/core/classes/Array.html#M002173

And pass them to File.delete
http://ruby-doc.org/core/classes/File.html#M002536

Simo M. removed_[email protected] wrote:

If file junk*.cs exist

  • remove all junk*.cs files from dir

Here it is :

#! /opt/local/bin/ruby1.9

encoding: utf-8

TRASH="#{ENV[‘HOME’]}/.Trash" #might be specific to Mac OS X
Dir.glob(“test/junk*.cs”).each {|file| File.rename(file,
“#{TRASH}/#{File.basename(file)}”)}

instead if u don’t want to trash, just remove :


#! /opt/local/bin/ruby1.9

encoding: utf-8

require ‘rubygems’
require ‘fileutils’

Dir.glob(“test/junk*.cs”).each {|file| FileUtils.rm_rf file}

On Wed, Oct 20, 2010 at 9:16 AM, Josh C. [email protected]
wrote:

but I don’t think there is a way to specify multiple digits, so something
http://ruby-doc.org/core/classes/Enumerable.html#M003121

Then use each to iterate over the list of file names
http://ruby-doc.org/core/classes/Array.html#M002173

And pass them to File.delete
http://ruby-doc.org/core/classes/File.html#M002536

If you drop explicit matching of digits there is a much easier approach:

09:36:22 test$ touch junk1.cs junk2.cv
09:36:23 test$ irb19
Ruby version 1.9.1
irb(main):001:0> f = Dir[“junk*.c[sv]”]
=> [“junk1.cs”, “junk2.cv”]
irb(main):002:0> File.unlink f
=> 2
irb(main):003:0> Dir["junk
.c[sv]"]
=> []
irb(main):004:0>

Or as one liner

File.unlink(Dir["junk.c[sv]"])

Kind regards

robert

2010/10/20 Une B. [email protected]:

Robert K. [email protected] wrote:

File.unlink(Dir["junk.c[sv]"])
^_____

where did u find this “*” syntax ?
really fine !

Which star do you mean, the first one or the second one? I can’t
really tell in my mail reader since Google Mail at some point dropped
the “show in fixed width font” option.

First one:

That’s the splash operator which in this case unrolls an array
argument to multiple method arguments.

Second one:

Ruby’s documentation is bad in places - but not that bad. Try

ri19 ‘Dir.[]’
ri19 ‘Dir.glob’

Cheers

robert

Robert K. [email protected] wrote:

File.unlink(Dir["junk.c[sv]"])
^_____

where did u find this “*” syntax ?
really fine !

On 20.10.2010 12:56, Une B. wrote:

Robert K.[email protected] wrote:

File.unlink(Dir["junk.c[sv]"])
^_____

where did u find this “*” syntax ?
really fine !

Somehow my reply did not make it to the news group.

That’s the splash operator. You should find it in any good Ruby book.

a = […] # an Array
x, y, z = *a

Kind regards

robert

On Sat, Oct 23, 2010 at 9:50 AM, Robert K.
[email protected]wrote:

That’s the splash operator. You should find it in any good Ruby book.

Here’s some coverage on splat: http://ruby.runpaint.org/variables#splat

This book is mainly for 1.9, but I don’t believe splat’s semantics have
changed since 1.8.

Regards,
Ammar

Robert K. [email protected] wrote:

That’s the splash operator. You should find it in any good Ruby book.

a = […] # an Array
x, y, z = *a

Kind regards

fine, thanks !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs