2

At work I have to repeat this same process multiple times:

  1. Open a certain Dreamweaver file.
  2. Look for all <p> tags and replace then with <h1> tags.
  3. Look for all </p> and replace with </h1>.
  4. Look for the string 'Welcome' and replace with 'goodbye'.
  5. Look for '0:01:00' and replace with '01:00'.
  6. Copy everything in that file.
  7. Create a new Dreamweaver file and paste everything in the new file.
  8. Save the new file in a given directory and call it a certain name, which can be provided as a variable.

I don't need to run the JavaScript from a browser. It can be a JavaScript file which I just double click on the desktop.

Is it possible for me to do this with JavaScript / jQuery?

4
  • No I don't think you can. Why don't you use Dreamweavers find and replace function? Commented Jan 23, 2014 at 23:37
  • 2
    It's definitely possible using, e.g. node.js and various libraries, but that's way too much work. Much easier from the command line using, e.g. sed Commented Jan 23, 2014 at 23:39
  • it might also be possible using the browser, see gist.github.com/abicky/1089708 and developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O. I would not recommend it though, it seems labourious. Commented Jan 23, 2014 at 23:41
  • @putvande because I need to do it for multiple files and even more files down the road. Would be easy if there is just a script which I can run which will go through all the files and do the same thing to all the files in, say a given directory. Commented Jan 24, 2014 at 1:42

1 Answer 1

2

There are many other programming languages that you could accomplish this task with but if you really want to use Javascript then you could do the following:

var fs = require('fs');

if(process.argv.length < 4) {
  console.log('Usage: node replace.js fromFilePath toFilePath');
  return;
}

from = process.argv[2];
to = process.argv[3];

fs.readFile(from, { encoding: 'utf-8' }, function (err, data) {
  if (err) throw err;
  console.log('successfully opened file ' + from);
  var rules = {
    '<p>': '<h1>',
    '</p>': '</h1>',
    'Welcome': 'goodbye',
    '0:01:00': '01:00'
  };

  for(var index in rules) {
      console.log('Replacing ' + index + ' with ' + rules[index] + '...');
      data = data.replace(new RegExp(index, 'gi'), rules[index]);
      console.log('Done');
  }

  console.log("Result");
  console.log(data);

  console.log("Writing data to " + to);

  fs.writeFile(to, data, function (err) {
    if (err) throw err;
    console.log('It\'s saved!');
  });

});

INSTRUCTIONS

  1. Download node.js from here
  2. Install it
  3. Create a file in C:\replace.js (Win) or ~/replace.js (Mac OS)
  4. Put the code from above in replace.js
  5. Open cmd (Ctrl+R on Win) or Terminal (on Mac OS)
  6. Type node C:\replace.js <fileToReadFrom> <fileToSaveTo> on Win or node ~/replace.js <fileToReadFrom> <fileToSaveTo> on Mac OS
  7. Done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.