我一整天都在寻找从本地文件系统读取二进制文件的方法,对其进行一些修改并将其保存回磁盘。该脚本必须批量运行,因为之后必须运行其他脚本。
读取不是问题:您始终可以将其作为文本读取,然后将字符转换为字节。问题是将其写入磁盘。无法使用ActiveXObject("Scripting.FileSystemObject")
工具,因为某些字节不会映射到字符,然后Write
方法会抛出异常。
我已经阅读过帖子,其他地方建议使用ADODB.Stream
这是我可以去的地方:
var foo = ...
var stream = new ActiveXObject('ADODB.Stream');
stream.Type = 1; //Means "binary".
stream.Open();
stream.Write(foo);
stream.SaveToFile('C:\\foo.bin', 2); //2 means save/create/overwrite
我在foo
中放入了哪种类型的变量,Windows脚本宿主声称:
Error: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Code: 800A0BB9
Source: ADODB.Stream
似乎ADODB Stream.Write方法需要A Variant that contains an array of bytes to be written。由于Javascript中不存在这样的东西,我尝试使用数字,字符串,单个数字,单个字符,十六进制表达式的数组...我已找到VBArray但是你实际上不能写任何东西。
有人知道必须使用哪种类型?还是保存二进制数据的其他意思?
答案 0 :(得分:0)
改编自ADODB.Stream writing binary file, JScript only。注释在代码中进行了解释。
var adTypeBinary = 1
var adTypeText = 2
var adSaveCreateOverWrite = 2
var stdout = WScript.StdOut;
// Read a binary file, particularly for debugging purposes
var inStream = new ActiveXObject("ADODB.Stream");
inStream.Type = adTypeBinary;
inStream.Open();
inStream.LoadFromFile("d:\\bat\\cliParser.exe");
inStream.Position = 0;
var binData = inStream.Read();
inStream.Close();
stdout.WriteLine( "binData " + typeof(binData)); // returns: "undefined"
// Convert binary value of "undefined" data type to "string" data type
var objRS = new ActiveXObject("ADODB.Recordset");
var DefinedSize = 1024; /* A Long value that represents the defined size,
in characters or bytes, of the new field. Fields that have a DefinedSize
greater than 255 bytes are treated as variable length columns. */
var adFldLong = 0x80; /* Indicates that the field is a long binary field.
Also indicates that you can use the AppendChunk and GetChunk methods. */
var adVarChar = 201; /* Indicates a long string value. */
objRS.Fields.Append("test", adVarChar, DefinedSize, adFldLong);
objRS.Open();
objRS.AddNew();
objRS.Fields("test").AppendChunk(binData);
var binString = objRS("test").value;
objRS.close();
stdout.WriteLine( "binString " + typeof(binString)); // returns: "string"
// String is now manipulable (unlike "undefined" data type)
// Write string to a file (converting string in one-byte encoding schema)
var outStreamW = new ActiveXObject("ADODB.Stream");
outStreamW.Type = adTypeText;
// Charset: the default value seems to be `UTF-16` (BOM `0xFFFE` for text files)
outStreamW.Open();
outStreamW.WriteText(binString);
outStreamW.Position = 0;
var outStreamA = new ActiveXObject("ADODB.Stream");
outStreamA.Type = adTypeText;
outStreamA.Charset = "windows-1252"; // important, see `cdoCharset Module Constants`
outStreamA.Open();
outStreamW.CopyTo(outStreamA); // convert encoding
outStreamA.SaveToFile("D:\\test\\fooRus.exe", adSaveCreateOverWrite);
outStreamW.Close();
outStreamA.Close();
// Done. Make certain of input and output file oneness!
<强>输出强>:
==> cscript D:\VB_scripts\JScripts\33330187my.js
binData unknown
binString string
==> echo n|COMP "d:\bat\cliParser.exe" "D:\test\fooRus.exe" 2>NUL
Comparing D:\bat\cliParser.exe and D:\test\fooRus.exe...
Files compare OK
==>