By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Update lastmod in sitemap.xml file

P: 2
I have written a few scripts for cron jobs on my website, but I am NO expert

I am trying to write a perl script that will read the last modified date of the .shtml files on the root of my server and update them in my sitemap_test.xml file also in the root.

My first step was to get the script to gather and print the info I need so later I could write it to the sitemap_text .xml file.

I was able to get the .shtml file names, the modified date in the right format for the .xml file. Then open the file match the .shtml file name in the .xml file but I can't get perl to pull the next line that contains the <lastmod>.+</lastmod> that I need for the variable to write the modification date into.

Note: the sitemap_test.xml is Windows (CRLF). Here is a link to it http://saildallas.com/sitemap_test.xml

Here is the code

Expand|Select|Wrap|Line Numbers
  1.  
  2. use warnings;
  3. use strict;
  4. use POSIX;
  5.  
  6.  
  7. # Gather the .shtml files in n array #
  8. my @shtml_files = <*\.shtml>;
  9.  
  10. # The Sitemap file
  11. my $filename = "sitemap_test.xml";
  12.  
  13. foreach my $shtml_files (@shtml_files)
  14. {
  15.         my $lastmod = strftime("%Y-%m-%dT%H:%M:%S-06:00", localtime((stat($shtml_files))[9]));
  16.         print "\n$shtml_files\n";
  17.         print "$lastmod\n";
  18.  
  19.         open ( MYFILE, $filename) or die "Cannot open file: $!";
  20.             while (<MYFILE>) 
  21.             {
  22.                 my $string= $_;
  23.                 #$string =~ s/\s+$//;# Strip off all trailing white spaces - tabs, spaces, new lines and returns as well.
  24.                 #print "$string";
  25.  
  26.  
  27.                 if ($string =~ m/$shtml_files/)
  28.                 {
  29.                        print "Found .shtml = $_";
  30.                        my $nextLine = <MYFILE>;
  31.                      #$nextLine =~ s/\s+$//;
  32.                      print "Found lastmod = $nextLine\n";
  33.                    }
  34.  
  35.                    #if (m/<lastmod>.+<\/lastmod>/)
  36.                    #{
  37.                    #    print "$_";
  38.                    #}
  39.  
  40.                 }
  41.  
  42.  
  43. }
  44.  
  45.  
Thanks in advance for any help.
Apr 6 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
Okay, I've got it to pull all of the data I need to substitute the lastmod date in the xml file but the s/// doesn't work, any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2. use warnings;
  3. use strict;
  4. use POSIX;
  5.  
  6. # Gather the .shtml files in an array #
  7. my @shtml_files = <*\.shtml>;
  8.  
  9. # The Sitmap file
  10. my $filename = "sitemap_test.xml";
  11.  
  12.  
  13. foreach my $shtml_files (@shtml_files)
  14. {
  15.     my $lastmod = strftime("%Y-%m-%dT%H:%M:%S-06:00", localtime((stat($shtml_files))[9]));
  16.     print "\n$shtml_files\n";
  17.     print "$lastmod\n";
  18.  
  19.         open ( MYFILE, "+<$filename") or die "Cannot open file: $!";
  20.         while (<MYFILE>)
  21.             {
  22.             if (m/$shtml_files/) 
  23.                 {
  24.                 print "got <loc> =$_\n";
  25.  
  26.                 my $nextLine = <MYFILE>;
  27.  
  28.                 my $nextLine1 = <MYFILE>;
  29.                 print "got <lastmod> = $nextLine1\n";
  30.  
  31.                 s/$nextLine1/<lastmod>$lastmod<\/lastmod>/;
  32.                 }
  33.  
  34.             }
  35.         close (MYFILE);
  36.  
  37. }
  38.  
  39.  
Apr 8 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.