Another way to read and write a binary file with a VBS is to share _vbs

Source: Internet
Author: User
Tags decrypt file size
Previously written that the method is good, but when the file is too large (in fact very small, also a few m), the corresponding array will be very large, and then there will be run-time errors, prompting memory exhaustion, so I was depressed for a long time.

Recently in the blog Traffic Records found in Baidu keyword "some very mischievous VBS program" (it seems that the young people are very like to play this mouth), so opened a long-lost Baidu search for a moment, found in the third. Appearing in the second is Baidu Hi, which is also a hoax program, but also encrypted. I tried to decrypt, but found that the encryption seems to be more than the last few days to decrypt the virus more abnormal, not fruit.

Although I did not decrypt that program (if a real master would, trouble pointing), but learned from the use of a VBS to read and write binary files, another way, namely Microsoft.xmldom+adodb. Stream. This method is much more efficient than the original one and seems to have no file size limit.
Copy Code code as follows:

Function readbinary (FileName)
Const adTypeBinary = 1
Dim Stream, XMLDOM, node
Set xmldom = CreateObject ("Microsoft.XMLDOM")
Set node = xmldom. createelement ("binary")
Node. DataType = "Bin.hex"
Set stream = CreateObject ("ADODB.") Stream ")
Stream. Type = adTypeBinary
Stream. Open
Stream. LoadFromFile FileName
Node. nodeTypedValue = stream. Read
Stream. Close
Set stream = Nothing
readbinary = node. Text
Set node = Nothing
Set xmldom = Nothing
End Function

The parameter of the function is the file path to read, and the return value is a string containing the hexadecimal value of the binary file.
Copy Code code as follows:

Sub writebinary (FileName, Buf)
Const adTypeBinary = 1
Const Adsavecreateoverwrite = 2
Dim Stream, XMLDOM, node
Set xmldom = CreateObject ("Microsoft.XMLDOM")
Set node = xmldom. createelement ("binary")
Node. DataType = "Bin.hex"
Node. Text = Buf
Set stream = CreateObject ("ADODB.") Stream ")
Stream. Type = adTypeBinary
Stream. Open
Stream.Write node. nodeTypedValue
Stream.savetofile FileName, Adsavecreateoverwrite
Stream. Close
Set stream = Nothing
Set node = Nothing
Set xmldom = Nothing
End Sub

The first parameter is the file path to be read, and the second is a string containing the hexadecimal value of the binary file. As for how to get the hexadecimal value of the binary file, the method is much, the simplest way of course is to use the FSO object to Readbinary function return value to the text file, and then Ctrl+c,ctrl+v. In fact, using the 16 tool is also very simple.

The others don't say much, and look at the MSDN reference documentation. Wrote an example program, which contains all the data of the Sky City Music, double click to generate the MP3 file in the Sky City and play it in the same folder.

Original: http://demon.tw/programming/vbs-binary-file-another.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.