Removing files if those exists

S

Simo M.

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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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

Robert Klemme

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
=A0- remove all junk*.cs files from dir

Thanks in advance.
First select them using Dir[] (you can try to get fancy with a pattern he= re,
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 us= e
the pattern above. You'll have to give it a regexp, if you aren't familia= r
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

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 =3D Dir["junk*.c[sv]"]
=3D> ["junk1.cs", "junk2.cv"]
irb(main):002:0> File.unlink *f
=3D> 2
irb(main):003:0> Dir["junk*.c[sv]"]
=3D> []
irb(main):004:0>

Or as one liner

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

Kind regards

robert


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

Une Bévue

Simo M. said:
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}
 
R

Robert Klemme

Robert Klemme said:
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
 
U

Une Bévue

Robert Klemme said:
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 !
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top