Skip to content Skip to sidebar Skip to footer

Run Javascript On Multiple Html Files

I am having a folder with 1000 html files. I have to remove certain nodes using xpath from each html. SO i have made the javascript. I cant open each file and run the javascript th

Solution 1:

Javascript can be run on a Linux server using nodejs.

Here are the javascript APIs for accessing the file system in nodejs.

http://nodejs.org/api/fs.html

You will also need an xpath library for nodejs. There appear to be several. https://nodejsmodules.org/tags/xpath

Nodejs is sometimes confusing to a newcomer because while it is single threaded it is also designed to be asynchronous, asking for a callback function to call when the information is available and returning immediately. Many of the file system calls have a "sync" in the name. These are "synchronous" and might be more suitable if your automatic editing application is mostly already written in a synchronous style.

Solution 2:

This would probably be easier to do with any server side language.

You can make a PHP file which reads the directory where your 1000 HTML files are, loads them one by one changes them, and saves them.

For example:

if ($handle = opendir('projects')) {
    while (false !== ($entry = readdir($handle))) {
      $file = file_get_contents($entry);
      $file = // make your changes here
      file_put_contents($entry, $file);
    }
  }
  closedir($handle);
}

Solution 3:

This can help to go through node.js solution: http://www.youtube.com/watch?v=w6kD6BNW4GE

Post a Comment for "Run Javascript On Multiple Html Files"