Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1064205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:53:48+00:00 2026-05-16T18:53:48+00:00

when we use Expires header for text files like js, css, contents are cached

  • 0

when we use Expires header for text files like js, css, contents are cached in the browser, to get new content we need to change in the html file the new names in the link and script tag. When we add changes. How can we automate it.

In a Windows Box, I may have some bunch of html files in multiple folders also in subdirectories.

There would be a text file

filelist.txt

OldName                 NewName
oldfile1-ver-1.0.js      oldfile1-ver-2.0.js  
oldfile2-ver-1.0.js      oldfile2-ver-2.0.js  
oldfile3-ver-1.0.js      oldfile3-ver-2.0.js  
oldfile4-ver-1.0.js      oldfile4-ver-2.0.js  

The script should change all the oldfile1-ver-1.0.js into oldfile1-ver-2.0.js in the html, php files

I would run this script before i start uploading.

Finally the script could create a list of files and line number where it made the update.

The solution can be in Perl/PHP/BATCH or anything thats nice and elegant

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:53:49+00:00Added an answer on May 16, 2026 at 6:53 pm

    The code below uses Perl’s in-place editing feature to modify only those files that need changing and leaves backups of the originals. For example, if it updates foo.php, the original will be in foo.php.bak.

    We begin with typical front matter. We’ll use the File::Find module to search for HTML and PHP files at any depth, starting by default in the current directory or in all directories named on the command line.

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use File::Find;
    
    sub usage { "Usage: $0 [ directory .. ]\n" }
    

    In read_filelist, we get the list of transformations use them to generate two bits of code:

    1. a compiled regex that matches any line containing at least one old filename, and
    2. a compiled sub that replaces old names with new.

    Note the use of \b which matches only at word boundaries and quotemeta, which escapes any regex metacharacters. These constrain the replacements to only literal occurrences of the old filenames.

    It’s a bit lengthy, so pardon the scrollbar.

    sub read_filelist {
      my $path = "filelist.txt";
      open my $fh, "<", $path
        or die "$0: open $path: $!";
    
      my $code = q[
        sub {
          while (<>) {
      ];
    
      my @old;
      my $errors;
      while (<$fh>) {
        next if $. == 1 && /OldName.*NewName/;
    
        my($old,$new) = split;
        unless (defined($old) && defined($new)) {
          warn "$0: $path:$.: missing filename\n";
          ++$errors;
        }
    
        my($qmold,$qmnew) = map quotemeta($_) => $old, $new;
        $code .= "        s/\\b$qmold\\b/$qmnew/gi;\n";
    
        push @old, $old;
      }
    
      die "$0: will not continue\n" if $errors;
    
      $code .= q[        print; 
          }
        }];
    
      local $@;
      print $code, "\n";
      my $sub = eval $code;
      die "$0: should not happen: $@" if $@;
    
      my $pattern = '\b(' .
                    join("|", map quotemeta, @old) .
                    ')\b';
    
      #print $pattern, "\n";
      my $regex = eval { qr/$pattern/oi };
      die "$0: should not happen: $@" if $@;
    
      ($regex, $sub);
    }
    

    Here we remember the root directories to begin searching and read filelist.txt. Perl’s in-place editing requires the files to be updated to be in the special @ARGV array.

    my @dirs = @ARGV ? @ARGV : ".";
    my($has_old,$replace) = read_filelist;
    

    The sub needs_update is the worker that File::Find::find uses to check whether a given file should be modified. We could place all PHP and HTML files in @ARGV, but in cases when the code modifies nothing, you’d still get backup files for everything.

    sub needs_update {
      return unless /\.(?:php|html)$/i;
    
      open my $fh, "<", $_
        or warn("$0: open $_: $!"), return;
    
      while (<$fh>) {
        if (/$has_old/) {
          push @ARGV, $File::Find::name;
          return;
        }
      }
    }, 
    

    For the main event, we clear @ARGV, use needs_update to add the appropriate PHP and HTML files, and unleash the compiled sub on them.

    # set up in-place editing
    @ARGV = ();
    find \&needs_update, @dirs;
    
    die "$0: nothing to do!\n" unless @ARGV;
    $^I = ".bak";
    $replace->();
    
    __END__
    

    A couple of notes:

    • The code above attempts to make all replacements in the order given in filelist.txt.
    • It makes no attempt to limit scope to <link> and <script> elements. Doing this correctly requires an HTML parser. If names of Javascript sources in filelist.txt should not be replaced sometimes, I can update this answer with the extra machinery, but doing would add even more code.

    On Windows, paste the code (minus my running commentary) into a file with an extension of .pl, and then run it as

    C:\> fixjs.pl

    to patch HTML and PHP files beneath the current directory. To process one or more directories elsewhere, run

    C:\> fixjs.pl D:\some\dir E:\another
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to copress my JavaScript and CSS files and I use a
I'd like to set the Expires header for js.gz files. I've added a .htaccess
I need to get the contents of a page, which always sends a Content-Length:
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
use Text::Diff; for($count = 0; $count <= 1000; $count++){ my $data_dir=archive/oswiostat/oracleapps.*dat; my $data_file= `ls
use LWP::UserAgent; use Data::Dumper; my $ua = new LWP::UserAgent; $ua->agent(AgentName/0.1 . $ua->agent); my $req
I have a dynamically generated CSS file. It's fairly large, and has different content
Godaddy won't enable mod_gzip for me, so I have to use headers like this:
I'm using the following script to initiate file downloads: if (file_exists($newfilename)) { header('Content-Description: File
I have one issue with my web project we use to release a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.