I need to append a string of text to the end of a binary file.
This is what I'm trying:
inStream = WScript.CreateObject("ADODB.Stream") ;
inStream.type = 1 ;
inStream.open() ;
inStream.LoadFromFile('test.bin') ;
outStream = WScript.CreateObject("ADODB.Stream") ;
outStream.type = 1 ;
outStream.open() ;
outStream.write( inStream.read() ) ;
outStream.write( "\nCONTENT AT THE END" ) ; // this gives an error
outStream.SaveToFile('test2.bin',2) ;
The reported error is "wrong argument".
The documentation of that method says the argument must be of type variant
.
How can I convert a string to a variant?
The solution is to use auxiliary ADODB.Stream
instance .copyTo()
method.
var inStream = WScript.CreateObject('ADODB.Stream'); // source stream
inStream.Type = 1; // adTypeBinary
inStream.Open();
inStream.LoadFromFile('C:\\Test\\src.bin');
var outStream = WScript.CreateObject('ADODB.Stream'); // target stream
outStream.Type = 1; // adTypeBinary
outStream.Open();
outStream.Write(inStream.read());
inStream.Close();
var bufStream = WScript.CreateObject('ADODB.Stream'); // auxiliary stream
bufStream.Type = 2; // adTypeText
bufStream.Open();
bufStream.WriteText('\nCONTENT AT THE END'); // strings held as Unicode in memory
bufStream.Position = 2; // skip BOM bytes FF FE
bufStream.CopyTo(outStream); // append to the end of target stream
bufStream.Close();
outStream.SaveToFile('C:\\Test\\dst.bin', 2);
outStream.Close();