I want make an irssi script which will notice of new files on server, but firstly I need a script in Perl which will return the newest file on the server (apache 2.2.16 with directory listing) and after the next script runs if there is no newer file then previous returns null or if there is a new file on the server return the link to that file. Thanks for any help.
Share
Remember the script’s start time in a variable
$start_timeYou will need to store “last run time” in some persistent way (cache, file, persistent session data, database/DBM file) later on.
Have the script generate the list of files you want (using
glob()if it’s just files in a given directory, orFile::Findif it’s recursive in a directory structureFor each file, find its creation or modification (whichever one you want) timestamp via
statcallFind the newest file using
statdata. If you usedglobto list files, do it by scanning the list; ifFile::Find, just do it as part of callback memorizing the “latest seen so far” fileRetrieve “last run time” persistent data
If the last file found is later than “last run time” , return link to that file. if not, return null
Save the start time (
$start_time) of the script as a new value of “last run time” to wherever you chose to tore it.UPDATE
If the list of files is NOT local (e.g. on a web server), you need to replace the
globstep to list the files (andstatfor timestamps) with scraping of the appropriate HTML page containing directory listing (using for exampleWWW::Mechanizeto retrieve the page andHTML::TwigorHTML::Parserto parse the HTML and retrieve the filenames and timestamps. There are also modules specifically for reading contents of HTML tables and I wouldn’t be surprised if CPAN has a module to parse specifically Apaches file listing HTML.UPDATE2
Looks like my hunch was right and there’s a possible module to parse the HTML output from mod_autoindex:
File::Listing::apache(part oflibwww-perldistribution)