0

I'm trying to write a script that removes temporary files (ie. files with a tilde at the end: temp.txt~)

#!/usr/bin/ruby
system("rm *~") if File::exists?( "*~" );

but that literally looks for files named *~.

I tried something like this:

#!/usr/bin/ruby                                                                                                                                                                                                  
if File::exists?( /\w*~/ )
  puts "Temp exists\n";
else
  puts "Temp doesn't exist\n";
end

but that gives me this:

in `exists?': can't convert Regexp into String (TypeError)

How should I correctly search and remove these files?

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Jessehk
0

I can't say that this is "correct', but this is the way I'd approach the problem. :)

#!/usr/bin/ruby

def yes_no(question)
    print question
    
    if gets.chomp =~ /^[yY]/
        return true
    else
       return false
    end
end

delete_list = []

Dir.new(".").each do |file|
    if file =~ /\w+\~/
        delete_list << file
    end
end

puts delete_list.join("\n")
puts '---------------'
if yes_no("delete the previous files? <y/n>: ")
    delete_list.each { |file| File.delete(file) }
end
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.