logo.jpg (4128 bytes)
Home
Main Page
Links
Classical
Popular
Programming
Shareware
Emulation
Opus
Gtr & Fl.
Concerto
Code
Status bar
Math
Perl
Base Address
Meta Script
DHTML
WSC
Programs
Markup
Clipboard

 

Meta Tag Script

I 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 below

arrow.jpg (1679 bytes) Meta_Script.zip

The 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>

 

Aaron L. Stephanus Do YOU want YOUR choice of a FREE laptop ?