At work I have to repeat this same process multiple times:
- Open a certain Dreamweaver file.
- Look for all
<p>tags and replace then with<h1>tags. - Look for all
</p>and replace with</h1>. - Look for the string
'Welcome'and replace with'goodbye'. - Look for
'0:01:00'and replace with'01:00'. - Copy everything in that file.
- Create a new Dreamweaver file and paste everything in the new file.
- 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?
dana
18.3k7 gold badges71 silver badges91 bronze badges
asked Jan 23, 2014 at 23:34
user2817200
1,1272 gold badges18 silver badges39 bronze badges
-
No I don't think you can. Why don't you use Dreamweavers find and replace function?putvande– putvande2014年01月23日 23:37:52 +00:00Commented Jan 23, 2014 at 23:37
-
2It'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. sedStephen Thomas– Stephen Thomas2014年01月23日 23:39:21 +00:00Commented 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.Jorg– Jorg2014年01月23日 23:41:47 +00:00Commented 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.user2817200– user28172002014年01月24日 01:42:24 +00:00Commented Jan 24, 2014 at 1:42
1 Answer 1
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
- Download node.js from here
- Install it
- Create a file in
C:\replace.js(Win) or~/replace.js(Mac OS) - Put the code from above in
replace.js - Open
cmd(Ctrl+R on Win) orTerminal(on Mac OS) - Type
node C:\replace.js <fileToReadFrom> <fileToSaveTo>on Win ornode ~/replace.js <fileToReadFrom> <fileToSaveTo>on Mac OS - Done
answered Jan 24, 2014 at 1:01
vooD
2,9212 gold badges26 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js