From: Ben Giddings Date: 2007-10-17T13:33:56+09:00 Subject: Re: Mind giving a little help? Ok, your functions would be: require 'uri' require 'net/http' # Check to see if a URL exists, returning true if it does, false if it doesn't def image_exists?(url_string) # Create a "URI" object from the string-form URL url = URI.parse(url_string) # connect to the web server listed in the URL Net::HTTP.start(url.host, url.port) do |http| # Ask the web server for the HTTP header for something matching the path response = http.head(url.path) case response # If we get a successful response back (i.e. the server knows about the path) when Net::HTTPSuccess, Net::HTTPRedirection # Look at the content_type, and see if it's an image case response.content_type when "image/png", "image/gif", "image/jpeg" # If both of these check out, the image exists at that URL return true end end end # If we get to here, we have to assume the image doesn't exist return false end # Ask the user if he/she wants to save the URL def user_wants_to_save?(url) # Print out the question print "Save #{url.chomp}? [y/N]: " # read in the answer input = $stdin.gets # if the answer contains a y (either upper-case or lower-case) if /y/i =~ input return true else return false end end # Open a new output file named "urls_checked.txt" and # create a handle 'out' so we can interact with it File.open("urls_checked.txt", "w") do |out| # Open the input file "urls.txt" and read it one line at a time # calling each line of the file 'url' as we read it File.foreach("urls.txt") do |url| # See if the URL on this line of the file exists if image_exists?(url) # If the URL exists, ask if the user wants to save it if user_wants_to_save?(url) # If so, print that URL into the output file out.puts url end # If the user didn't want to save the URL, do nothing # it won't show up in the output file else # If the URL doesn't exist, add it to the output file out.puts url end end end # Optional -- overwrite the original file with the file containing checked URLs FileUtils.mv("urls_checked.txt", "urls.txt") On Oct 16, 2007, at 22:49, Tj Superfly wrote: > I've created 2 files with the names you have there, like you > suggested. > Here are their locations. This is actually what I don't understand the > most is this whole file thing, I think that's what throwing me off the > most. Anyway, here are their locations: > > C:\Documents and Settings\nonstickglue\My Documents\TDN\urls.txt > C:\Documents and Settings\nonstickglue\My Documents\TDN > \urls_checked.txt So urls.txt is where you should put the list of all the urls you want to check. Don't create urls_checked.txt, the program will create it and place the output there. Take what i wrote above and see if you can figure out what's going on. If you have questions post them. Ben