- AuthorPosts
- December 20, 2008 at 9:11 pm #6787nickbMember
I have 100 html files that I want to save with the word between
and
as name.
ex.
1. I have 0001.html with insideemeditor help
2. result: emeditor-help.html
other example with just one word
1. I have hello.html with inside
screenshots
2. result: screenshots.html
I know editor.ExecuteCommandByID(4100); stands for ‘save as’ in a jsee file. But anyone with a good example?
gr.
December 22, 2008 at 6:51 pm #6792dreftymacParticipantSave the following example to h1_rename.jsee in your macro directory. Then open an html file that you want to rename. Run h1_rename.jsee while that file is open and the output file will be saved (copied) with the new name in the same directory.
// helper function that we use to put the entire text of a file in a string
// **************************************************
function string_fromfile(sUrl){
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var fso, fob;
try{
fso = new ActiveXObject("Scripting.FileSystemObject");
fob = fso.OpenTextFile(sUrl, ForReading);
var strRead = fob.ReadAll();
fob.Close();
fso = null;
return strRead;
}
catch(e){
if(000){}
else if(e.message == 'Input past end of file'){return '';} // blank file
else if(e.message == 'File not found'){return ''} // not found
else if(e.message != ''){return ''} // other problem
};
}
// main code
// **************************************************
stext = (string_fromfile(document.FullName));
smatch = '';
// look inside the raw text and see if we can find something inside the h1 tag
try{
amatch = stext.match(/<h1>([^<]+)<.h1>/);
smatch = amatch[1];
}catch(ex){ smatch = '';}
// did we actually find something inside the h1 tag?
if(smatch != ''){
// fix what we found so that unwanted characters get changed into a dash
smatch = smatch.split(/[W]+/).join('-');
// extract the parent directory and combine it with what we found to form a new filename
newname = (document.FullName.split(/x5c/).slice(0,-1).join("x5c") + "x5c" + smatch + '.htm');
// save a copy of the document with its new name
document.Save( newname );
}
NOTE: Using this script could be cumbersome and annoying. The reason is because it would be tedious to open 100 files, one at a time, and then run this macro against the file. Therefore, you may want to modify this macro to loop through the directory, grab all the names and save the files. Another problem, however, is it may be annoying and cumbersome to have a script open EmEditor to do this.
Therefore, the easiest way to accomplish this task may be simply to write a WSH script file and have it process the files in a without using EmEditor to do it. That’s probably how I would do it anyway.
Nevertheless, you asked for an example and there it is!
December 23, 2008 at 1:33 am #6794nickbMemberDreftymac , you really made my day (again)! :-D
I made some small changes to fit my needs, like
smatch = amatch[1].toLowerCase()
Thanks again
gr.
- AuthorPosts
- You must be logged in to reply to this topic.