The script does not use any outside
libraries, thus keeping its execution simple. we will start by initializing
the arrays that we'll be using to keep track of our links and orphan
files . Next, we look to ensure that our links.txt file exists. If not,
then there isn't much point in continuing to run the script, so it
exits out with a nice error message . If links.txt does exist, then
we continue by opening the file and reading in all of the contents
line-by-line. You can change this to a comma-separated values (CSV)
file, but I prefer the readability of one link per line.
After the links have been stored in the
array links, the script begins to index every file in the current
working directory. The results will be stored in an array called
orphans . If there are subdirectories, the script will also index
those files. Presumably, you would run this in the root directory of
your web server to take full advantage of this script.
Now that the script has both the links
and local files indexed, it is time to start comparing the two
arrays, and see what's left . I called the second array orphans
because I will be deleting any entry that exists within the link
array. Whatever is left will be files not included on the
public-facing side of the web server.
The script ends by creating a file
called orphans.txt in the script's directory and writing the results
to that file . Finally, after the code block is finished, the file is
closed and the script finished.
links = Array.new orphans = Array.new dir_array = [Dir.getwd] unless File.readable?("links.txt") puts "File is not readable." exit end File.open('links.txt', 'rb') do |lv| lv.each_line do |line| links << line.chomp end end begin p = dir_array.shift Dir.chdir(p) Dir.foreach(p) do |filename| next if filename == '.' or filename == '..' if !File::directory?(filename) orphans << p + File::SEPARATOR + filename else dir_array << p + File::SEPARATOR + filename end end end while !dir_array.empty? orphans -= links File.open("orphans.txt", "wb") do |o| o.puts orphans end