如何创建二进制文件并将其保存到 WSH (JScript) 中的本地文件系统? [英] How can I create a binary file and save it to local file system in WSH (JScript)?

查看:36
本文介绍了如何创建二进制文件并将其保存到 WSH (JScript) 中的本地文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在寻找一种从本地文件系统读取二进制文件的方法,对其进行一些修改并将其保存回磁盘.脚本必须批量运行,因为其他脚本必须在之后运行.

I've been a whole day looking for a way to read a binary file from the local file system, do some modifications to it and save it back to disk. The script must be run on batch, since other scripts must be run afterwards.

阅读不是问题:您始终可以将其作为文本阅读,然后将字符转换为字节.问题是将其写入磁盘.不能使用 ActiveXObject("Scripting.FileSystemObject") 工具,因为某些字节没有映射到字符,然后 Write 方法抛出异常.我读过一篇文章,在其他地方建议使用 ADODB.Stream,这是我所能做到的:

Reading is not an issue: you can always read it as text, and then convert the characters to bytes. The problem is writting it to disk. ActiveXObject("Scripting.FileSystemObject") facility can't be used, since some bytes don't map to characters and then Write methods throw an exception. I've read a post, somewhere else suggesting to use ADODB.Stream and this is as far as I can go:

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 Script Host 声称:

Regardles of which type of variable I put in foo, Windows Script Host claims:

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 方法需要 包含要写入的字节数组的变体.由于 Javascript 中不存在这样的东西,我尝试使用一个由数字、字符串、单个数字、单个字符、十六进制表达式填充的数组......我发现 VBArray 但您实际上无法在其中写入任何内容.

It seems that ADODB Stream.Write method expects A Variant that contains an array of bytes to be written. Since such things doesn't exist in Javascript, I tried with an array filled with numbers, a string, a single number, a single char, an hex expression... I've found VBArray but you can't actually write anything in those.

有人知道必须使用哪种类型吗?或者有其他保存二进制数据的方法吗?

Does anybody know which type must be used? Or any other mean to save binary data?

推荐答案

改编自 ADODB.Stream 写入二进制文件, 仅限 JScript.注释以在代码中进行解释.

Adapted from ADODB.Stream writing binary file, JScript only. Commented for explanation in code.

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

==>

这篇关于如何创建二进制文件并将其保存到 WSH (JScript) 中的本地文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆