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.

···

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

···

On Wed, Oct 20, 2010 at 1:54 AM, Simo M. <simo.maatta@gmail.com> wrote:

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.

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

Here it is :

···

Simo M. <simo.maatta@gmail.com> wrote:

If file junk*.cs exist
- remove all junk*.cs files from dir

----------------------------
#! /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}
----------------------------
--
« Le meilleur moyen de tenir sa parole est de ne jamais la donner. »
  (Napoléon Bonaparte)

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

···

On Wed, Oct 20, 2010 at 9:16 AM, Josh Cheek <josh.cheek@gmail.com> wrote:

On Wed, Oct 20, 2010 at 1:54 AM, Simo M. <simo.maatta@gmail.com> wrote:

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.

--
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.
class Dir - RDoc Documentation

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.
module Enumerable - RDoc Documentation

Then use each to iterate over the list of file names
class Array - RDoc Documentation

And pass them to File.delete
class File - RDoc Documentation

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

______________^___________________

where did u find this "*" syntax ?
really fine !

···

Robert Klemme <shortcutter@googlemail.com> wrote:

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

--
« Le meilleur moyen de tenir sa parole est de ne jamais la donner. »
  (Napoléon Bonaparte)

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

···

2010/10/20 Une Bévue <unbewusst.sein@fai.invalid>:

Robert Klemme <shortcutter@googlemail.com> wrote:

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

______________^___________________

where did u find this "*" syntax ?
really fine !

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

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 20.10.2010 12:56, Une Bévue wrote:

Robert Klemme<shortcutter@googlemail.com> wrote:

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

______________^___________________

where did u find this "*" syntax ?
really fine !

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

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

···

On Sat, Oct 23, 2010 at 9:50 AM, Robert Klemme <shortcutter@googlemail.com>wrote:

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

fine, thanks !

···

Robert Klemme <shortcutter@googlemail.com> wrote:

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

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

Kind regards

--
« L'amour n'a pas de meilleur ministre que l'occasion. »
   (Miguel de Cervantès)