|
Meta Tag ScriptI once created a good number of pages for a site, and forgot to include the Keyword Meta tag in each and every one of them. I was greatly uninterested in inserting the tag by editing each one by hand. So, I wrote a Windows Scripting File to do it for me. I admit that writing the script probably took as long as it would have taken to edit each individually, if not longer, but writing the script was much more interesting, and less tedious. The script changes all of the HTML files in given directory to include the Meta tag then it does the same in the given directories sub-directories. You can download the script belowThe code is below:<job> <script language="JScript"> // Create objects to work with the File System var fso = new ActiveXObject( "Scripting.FileSystemObject" ); var sDir = new String( // INSERT THE PATH CONTAINING THE HTML FILES HERE!! ); var fsTopDir = fso.GetFolder( sDir.toString() ); var fsFolders = new Enumerator( fsTopDir.SubFolders ); // Used to prompt the user when script is finished. var wshShell = new ActiveXObject( "WScript.Shell" ); // Take care of the files in the initial directory. var nChanged = WriteFiles( new Enumerator( fsTopDir.Files ) ); // Work through the subdirectories for ( ; !fsFolders.atEnd(); fsFolders.moveNext() ) { var f = fsFolders.item(); if ( f.Files.Count > 0 ) nChanged += WriteFiles( new Enumerator( f.Files ) ); } wshShell.Popup( "Finished!" + '\r\n' + nChanged + " files were changed." ); delete wshShell; delete fsFolders; delete sDir; delete fso; // Helper function that performs the real work function WriteFiles( colFiles ) { var nCount = 0; // Constants for File reading and writing var ForReading = 1; var TristateUseDefault = -2; var ForWriting = 2; // Loop through all of the files for ( ; !colFiles.atEnd(); colFiles.moveNext() ) { var o = colFiles.item(); // Make sure the File has an extension if ( o.Name.lastIndexOf( "." ) > -1 ) { // Check the extension to ensure that the file // is a .htm or .html File if ( ( o.Name.substr(o.Name.lastIndexOf(".")) == ".htm" ) || ( o.Name.substr(o.Name.lastIndexOf(".")) == ".html" ) ) { // Create the meta tag var sMeta = new String( "<meta name=" + '\"' + "keywords" + '\"' + " content=" + '\"' // ENTER YOUR KEYWORDS HERE!! + '\"' + ">" ); // Read the file var fsTxt = o.OpenAsTextStream( ForReading, TristateUseDefault ); var sFileTxt = fsTxt.ReadAll(); fsTxt.close(); // find the appropriate section of the document to insert the tag // in this case It will be inserted on the line following the // <head> tag var sHeadTag = "<head>" var re = new RegExp( sHeadTag.toString(), "i" ); var nPos = sFileTxt.search( re ); if ( nPos > -1 ) { var sBefore = sFileTxt.substr( 0, nPos + sHeadTag.length ); var sAfter = sFileTxt.substr( nPos + sHeadTag.length ); sFileTxt = sBefore + '\r\n' + sMeta + '\r\n' + sAfter; // write the file fsTxt = o.OpenAsTextStream( ForWriting, TristateUseDefault ); fsTxt.write( sFileTxt ); fsTxt.close(); nCount++; } delete re; } } } return nCount; } </script> </job>
|
|