User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Ruby section within the Web Development category of DaniWeb, a massive community of 423,013 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,009 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Ruby advertiser: SELL YOUR PRODUCT TODAY !
Views: 2695 | Replies: 1
Reply
Join Date: Jul 2005
Posts: 20
Reputation: davidianstyle is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
davidianstyle's Avatar
davidianstyle davidianstyle is offline Offline
Newbie Poster

Help Pattern Matching Script

  #1  
Oct 14th, 2007
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?
.: davidianstyle :.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Ontario, Canada
Posts: 18
Reputation: Jessehk is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
Jessehk's Avatar
Jessehk Jessehk is offline Offline
Newbie Poster

Re: Pattern Matching Script

  #2  
Oct 14th, 2007
I can't say that this is "correct', but this is the way I'd approach the problem.

  1. #!/usr/bin/ruby
  2.  
  3. def yes_no(question)
  4. print question
  5.  
  6. if gets.chomp =~ /^[yY]/
  7. return true
  8. else
  9. return false
  10. end
  11. end
  12.  
  13. delete_list = []
  14.  
  15. Dir.new(".").each do |file|
  16. if file =~ /\w+\~/
  17. delete_list << file
  18. end
  19. end
  20.  
  21. puts delete_list.join("\n")
  22. puts '---------------'
  23. if yes_no("delete the previous files? <y/n>: ")
  24. delete_list.each { |file| File.delete(file) }
  25. end
--Jessehk
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Ruby Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Ruby Forum

All times are GMT -4. The time now is 4:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC