Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







Howto: Merge two files together

Posted by on Sunday, January 18, 2004 (PST)

Howto: Merge two text files together. Script appends one file to the bottom of the first.

 

A couple of examples merging two files together.


Example 1: - The long way 

 

// Create FileSystemObject - Needed for working with files  
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
// Open files  
var f1 = fso.OpenTextFile( "firstfile.txt", 1 );    // Open for reading 
var f2 = fso.OpenTextFile( "secondfile.txt", 1 );   // Open for reading 
var f3 = fso.OpenTextFile( "Output.txt", 2, true ); // Open for writing, create 

f3.Write( f1.ReadAll() );  // Write Data from first file to output
f3.Write( f2.ReadAll() );  // Write Data from second file to output
// Close files  
f1.Close(); f1 = null;
f2.Close(); f2 = null;
f3.Close(); f3 = null;


Example 2: - Shorter and in a function

 

// Call our function to append the 
// 'second' file to the 'first'
  
append( "first.txt", "second.txt" );
// function to merge two files together 
function append( topfile, bottomfile )
{
    // Error trapping
    try
    {
        // Create FileSystemObject
        var fso = new ActiveXObject( "Scripting.FileSystemObject" );
  
        // Open file for appending, don't create 
        var ftop = fso.OpenTextFile( topfile, 8, false ); 
  
        // Open for reading 
        var fbot = fso.OpenTextFile( bottomfile, 1 );
       
        // Append the contents of second file to first 
        ftop.Write( fbot.ReadAll() );
    }
    catch( e )
    {
        // Display any errors
        WScript.Echo( "Error: " + e.description );
    }
}

Add Your Comment



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon