Update lastmod in sitemap.xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skilodge
    New Member
    • Apr 2010
    • 2

    #1

    Update lastmod in sitemap.xml file

    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.xm l 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.xm l is Windows (CRLF). Here is a link to it http://saildallas.com/sitemap_test.xml

    Here is the code

    [CODE=Perl]

    use warnings;
    use strict;
    use POSIX;


    # Gather the .shtml files in n array #
    my @shtml_files = <*\.shtml>;

    # The Sitemap file
    my $filename = "sitemap_test.x ml";

    foreach my $shtml_files (@shtml_files)
    {
    my $lastmod = strftime("%Y-%m-%dT%H:%M:%S-06:00", localtime((stat ($shtml_files))[9]));
    print "\n$shtml_files \n";
    print "$lastmod\n ";

    open ( MYFILE, $filename) or die "Cannot open file: $!";
    while (<MYFILE>)
    {
    my $string= $_;
    #$string =~ s/\s+$//;# Strip off all trailing white spaces - tabs, spaces, new lines and returns as well.
    #print "$string";


    if ($string =~ m/$shtml_files/)
    {
    print "Found .shtml = $_";
    my $nextLine = <MYFILE>;
    #$nextLine =~ s/\s+$//;
    print "Found lastmod = $nextLine\n";
    }

    #if (m/<lastmod>.+<\/lastmod>/)
    #{
    # print "$_";
    #}

    }


    }

    [/CODE]

    Thanks in advance for any help.
  • skilodge
    New Member
    • Apr 2010
    • 2

    #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.

    [CODE=Perl]

    use warnings;
    use strict;
    use POSIX;

    # Gather the .shtml files in an array #
    my @shtml_files = <*\.shtml>;

    # The Sitmap file
    my $filename = "sitemap_test.x ml";


    foreach my $shtml_files (@shtml_files)
    {
    my $lastmod = strftime("%Y-%m-%dT%H:%M:%S-06:00", localtime((stat ($shtml_files))[9]));
    print "\n$shtml_files \n";
    print "$lastmod\n ";

    open ( MYFILE, "+<$filenam e") or die "Cannot open file: $!";
    while (<MYFILE>)
    {
    if (m/$shtml_files/)
    {
    print "got <loc> =$_\n";

    my $nextLine = <MYFILE>;

    my $nextLine1 = <MYFILE>;
    print "got <lastmod> = $nextLine1\n";

    s/$nextLine1/<lastmod>$lastm od<\/lastmod>/;
    }

    }
    close (MYFILE);

    }

    [/CODE]

    Comment

    Working...