Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建二进制文件并将其保存到WSH(JScript)中的本地文件系统?_Javascript_Adodb_Jscript_Wsh - Fatal编程技术网

Javascript 如何创建二进制文件并将其保存到WSH(JScript)中的本地文件系统?

Javascript 如何创建二进制文件并将其保存到WSH(JScript)中的本地文件系统?,javascript,adodb,jscript,wsh,Javascript,Adodb,Jscript,Wsh,我花了一整天的时间寻找一种方法,从本地文件系统读取二进制文件,对其进行一些修改,然后将其保存回磁盘。脚本必须在批处理上运行,因为其他脚本必须在批处理之后运行 阅读不是问题:您可以始终将其作为文本阅读,然后将字符转换为字节。问题是将其写入磁盘ActiveXObject(“Scripting.FileSystemObject”)facility无法使用,因为某些字节不映射到字符,然后Write方法会引发异常。 我读过一篇帖子,其他地方建议使用ADODB.Stream,我只能做到这一点: var fo

我花了一整天的时间寻找一种方法,从本地文件系统读取二进制文件,对其进行一些修改,然后将其保存回磁盘。脚本必须在批处理上运行,因为其他脚本必须在批处理之后运行

阅读不是问题:您可以始终将其作为文本阅读,然后将字符转换为字节。问题是将其写入磁盘
ActiveXObject(“Scripting.FileSystemObject”)
facility无法使用,因为某些字节不映射到字符,然后
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方法似乎需要。由于Javascript中不存在这样的东西,所以我尝试使用一个填充数字的数组、一个字符串、一个数字、一个字符、一个十六进制表达式。。。我发现了,但你不能用这些来写任何东西

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

改编自。注释以在代码中进行解释

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

==>