Strange behavioural difference between vbscript and jscript using ADODB.stream in an HTA application - vbscript

VBscript example:
Function ADO_WriteToFile(FileURL,data)
Dim arrBytes
SET ADObj = CreateObject("ADODB.Stream")
ADObj.Open
ADObj.Charset = "iso-8859-1"
ADObj.Type = adTypeText
ADObj.WriteText data
ADObj.SaveToFile FileURL, adSaveCreateOverwrite
ADObj.Close
ADObj.Open
ADObj.Type = adTypeBinary
ADObj.LoadFromFile FileURL
ADObj.Position = 3
arrBytes = ADObj.Read
ADObj.Position = 0
ADObj.SetEOS
ADObj.Write data
ADObj.SaveToFile FileURL, adSaveCreateOverwrite
ADObj.Close
End Function
JScript example:
function writeTo(fileName,str) {
var ado = new ActiveXObject("ADODB.Stream");
ado.Type = 2;
ado.Open();
ado.Position = 0;
ado.WriteText(str,0);
ado.SaveToFile(fileName,2);
ado.Close();
ado.Open();
ado.Type = 1;
ado.Position = 2;//line 19
var temp = ado.Read();
ado.Position = 0;
ado.SetEOS;
ado.Write(temp);
ado.SaveToFile(fileName,2);
ado.Close();
}
Why does the VBScript example work perfectly except for the fact that it can't accept file paths with space in them?
The JScript example errors out with the message "assignment to the parameter is incorrect." line 19. This doesn't happen if I set Position to 0 however:
ado.Position = 0;
i am using this to write binary files to disk btw

Here are some differences:
In the VBScript version, position is set to 3; in the JScript version, it is set to 2
In the VBScript version, the character set is defined; in the JScript version, it is undefined
In the VBScript version, WriteText and write both reference the argument; in the JScript version, only WriteText references it
References
Use vs Mention in JScript doesn't come for Free
JScript Data Types: Data Type Summary

Related

Error on attempt to create named pipe using VBScript

I'm trying to create named pipe using VBScript on win7.
This is my code (took from there):
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\.\pipe\PipeName", True)
a.WriteLine("This is a test.")
a.Close
But i got an error (manual translate, so may be not accurate):
test.vbs(2, 1) Microsoft VBScript runtime error: File not found
Same code with ordinary text file works fine:
Set a = fs.CreateTextFile(".\\PipeName", True)
But, when i tried to escape backslashes:
Set a = fs.CreateTextFile("\\\\.\\pipe\\PipeName", True)
I got:
test.vbs(2, 1) Microsoft VBScript runtime error: Path not found
UPD: I run script as administrator.
UPD2: I'm found another solution for my problem without using pipes, so my question is a little outdated, but I don't know what to do with it.
Did you create a named pipe server called 'PipeName'? This code works for me (I named my pipe 'HelloWorld'):
C# Server:
static void Main(string[] args)
{
using (var pipe = new NamedPipeServerStream("HelloWorld"))
{
pipe.WaitForConnection();
StreamReader reader = new StreamReader(pipe);
var line = reader.ReadLine();
Console.WriteLine(line);
}
Console.ReadLine();
}
VBScript Client:
Dim fs, pipe
Set fs = CreateObject("Scripting.FileSystemObject")
Set pipe = fs.OpenTextFile("\\.\pipe\HelloWorld", 8, False, 0)
pipe.WriteLine("This is my message")
pipe.Close
I tried to work with named pipe from VBScript. I failed to create named pipe by fso.CreateTextFile("\\.\pipe\MyPipe").
But I successfully connected from VBScript from with Pipe created by classical application:
Pipe was created by such code (pascal):
procedure OpenTestPipe;
var
i,hOut: Integer;
begin
hOut:=CreateNamedPipe('\\.\pipe\Test.htm',PIPE_ACCESS_OUTBOUND,PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,1024,1024,NMPWAIT_USE_DEFAULT_WAIT,nil);
i:=FileWrite(hOut,'Hello'#13#10,7);
MessageBox(0,'Pipe is opened','Pipe sample',0);
FileClose(hOut);
end;
When MessageBox was shown I opened VBScript
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.OpenTextFile("\\.\pipe\test.htm",1).readLine
And got a message Hello

Windows command-line disk cataloging - save to csv?

I'm working on a Windows batch script that creates a directory/file listing of a complete hard disk for archival/cataloging purposes, using only command line-tools (and open-source/free tools). For each of the entries in the listing I wanted to list the filename, directory where it resides in, the filesize, date a,nd time of the file, and the md5 sum. I have been able to create somewhat a working starting point, but I'm hitting a wall since I'm not sure if it is even possible using the command-line tools in Windows. The command "dir /s /a:-d /o:-d /t:c" gives me a nice overview, but I would like this overview displayed (or saved to) a comma-delimited format. So my questions are:
Can I create a csv file with all the fields I mentioned above, with the standard command-line tools (and a m5 freeware tool for the md5 sums)
Do you know of a better way, or is there a dead simple disk cataloging command-line tool I missed?
Thanks in advance for any tips!
You can use dir /s /a:-d /o:-d /t:c > slam.txt
Then the content of this slam.txt, can be processed by WScript in windows, making a CSV file ...
If you need a WScript ex, I can provide one ?
I know this not an CSV example - but it should be complex enough for pattern inspiration :)
and remember this fil is saved as .js
var what2lookfor = '<rect ';
var forReading = 1, forWriting = 2, forAppending = 8, jx = 0, ix = 0;
var triStateUseDefault = -2, triStateTrue = -1, triStateFalse = 0;
var thisRecord="", validFileTypes="js,xml,txt,php,xsl,css,htm,html" , akkum = "";
var fileArray = [];
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var objFiles = FSO.GetFolder("F:\\xps1710\\jscript\\");
var objFileControl = new Enumerator(objFiles.files);
for (; !objFileControl.atEnd(); objFileControl.moveNext()) {
objFile = FSO.GetFile(objFileControl.item());
var ext = objFile.Name.split(".");
if (validFileTypes.indexOf(ext[1]) > 1) {
fileArray[ix] = "F:\\xps1710\\jscript\\" + objFile.Name;
ix++;
}
}
for (zx = 0 ; zx < ix ; zx++ ) {
var file2Traverse = FSO.OpenTextFile(fileArray[zx], forReading, triStateUseDefault );
while (!file2Traverse.AtEndOfStream) {
thisRecord = file2Traverse.ReadLine();
if (thisRecord.indexOf(what2lookfor) > 1 ) {
akkum = akkum + fileArray[zx] + '::' + thisRecord + '\n';
break;
}
}
}
WScript.Echo(akkum);

VB6: Is there a standard library (parser) to strip HTML tags from content? [duplicate]

How to strip ALL HTML tags using MSHTML Parser in VB6?
This is adapted from Code over at CodeGuru. Many Many thanks to the original author:
http://www.codeguru.com/vb/vb_internet/html/article.php/c4815
Check the original source if you need to download your HTML from the web. E.g.:
Set objDocument = objMSHTML.createDocumentFromUrl("http://google.com", vbNullString)
I don't need to download the HTML stub from the web - I already had my stub in memory. So the original source didn't quite apply to me. My main goal is just to have a qualified DOM Parser strip the HTML from the User generated content for me. Some would say, "Why not just use some RegEx to strip the HTML?" Good luck with that!
Add a reference to: Microsoft HTML Object Library
This is the same HTML Parser that runs Internet Explorer (IE) - Let the heckling begin. Well, Heckle away...
Here's the code I used:
Dim objDocument As MSHTML.HTMLDocument
Set objDocument = New MSHTML.HTMLDocument
'NOTE: txtSource is an instance of a simple TextBox object
objDocument.body.innerHTML = "<p>Hello World!</p> <p>Hello Jason!</p> <br/>Hello Bob!"
txtSource.Text = objDocument.body.innerText
The resulting text in txtSource.Text is my User's Content stripped of all HTML. Clean and maintainable - No Cthulhu Way for me.
One way:
Function strip(html As String) As String
With CreateObject("htmlfile")
.Open
.write html
.Close
strip = .body.outerText
End With
End Function
For
?strip("<strong>hello <i>wor<u>ld</u>!</strong><foo> 1234")
hello world! 1234
Public Function ParseHtml(ByVal str As String) As String
Dim Ret As String, TagOpenend As Boolean, TagClosed As Boolean
Dim n As Long, sChar As String
For n = 1 To Len(str)
sChar = Mid(str, n, 1)
Select Case sChar
Case "<"
TagOpenend = True
Case ">"
TagClosed = True
TagOpenend = False
Case Else
If TagOpenend = False Then
Ret = Ret & sChar
End If
End Select
Next
ParseHtml = Ret
End Function
This is a simple function i mafe for my own use.
use Debug window
?ParseHtml( "< div >test< /div >" )
test
I hope this will help without using external libraries

How to strip ALL HTML tags using MSHTML Parser in VB6?

How to strip ALL HTML tags using MSHTML Parser in VB6?
This is adapted from Code over at CodeGuru. Many Many thanks to the original author:
http://www.codeguru.com/vb/vb_internet/html/article.php/c4815
Check the original source if you need to download your HTML from the web. E.g.:
Set objDocument = objMSHTML.createDocumentFromUrl("http://google.com", vbNullString)
I don't need to download the HTML stub from the web - I already had my stub in memory. So the original source didn't quite apply to me. My main goal is just to have a qualified DOM Parser strip the HTML from the User generated content for me. Some would say, "Why not just use some RegEx to strip the HTML?" Good luck with that!
Add a reference to: Microsoft HTML Object Library
This is the same HTML Parser that runs Internet Explorer (IE) - Let the heckling begin. Well, Heckle away...
Here's the code I used:
Dim objDocument As MSHTML.HTMLDocument
Set objDocument = New MSHTML.HTMLDocument
'NOTE: txtSource is an instance of a simple TextBox object
objDocument.body.innerHTML = "<p>Hello World!</p> <p>Hello Jason!</p> <br/>Hello Bob!"
txtSource.Text = objDocument.body.innerText
The resulting text in txtSource.Text is my User's Content stripped of all HTML. Clean and maintainable - No Cthulhu Way for me.
One way:
Function strip(html As String) As String
With CreateObject("htmlfile")
.Open
.write html
.Close
strip = .body.outerText
End With
End Function
For
?strip("<strong>hello <i>wor<u>ld</u>!</strong><foo> 1234")
hello world! 1234
Public Function ParseHtml(ByVal str As String) As String
Dim Ret As String, TagOpenend As Boolean, TagClosed As Boolean
Dim n As Long, sChar As String
For n = 1 To Len(str)
sChar = Mid(str, n, 1)
Select Case sChar
Case "<"
TagOpenend = True
Case ">"
TagClosed = True
TagOpenend = False
Case Else
If TagOpenend = False Then
Ret = Ret & sChar
End If
End Select
Next
ParseHtml = Ret
End Function
This is a simple function i mafe for my own use.
use Debug window
?ParseHtml( "< div >test< /div >" )
test
I hope this will help without using external libraries

Can't create object: ADODB.Stream

I am trying to create ADODB.Stream object in VBscript. This is the function:
Function ByteArray2Text(varByteArray)
'Convert byte array into a string with ADODB.Stream
'Data should be real plain text because binary data will be mangled
Dim byt
Const adTypeText = 2
Const adTypeBinary = 1
Set byt = CreateObject("ADODB.Stream")
byt.Type = adTypeBinary
byt.Open
byt.Write varByteArray
byt.Position = 0
byt.Type = adTypeText
byt.CharSet = "us-ascii"
ByteArray2Text = byt.ReadText
byt.Close
Set byt = Nothing
End Function
When I try to run this function i am getting error:
Microsoft VBScript runtime error: ActiveX component can't create object: 'ADODB.Stream'
What i need to do, to create this ADODB.Stream object?
Make sure that you have MDAC installed.
You also can try Microsoft Jet 4.0
You can also register these DLLs:
REGSVR32 "(path to "common files")\System\ole db\sqloledb.dll"
REGSVR32 "(path to "common files")\System\ole db\Oledb32.dll"
REGSVR32 "(path to "common files")\System\ole db\Msdasql.dll"
REGSVR32 "(path to "common files")\System\msadc\Msadce.dll"
They have relation with ADOdb
Make sure that:
The Stream component exits on your computer.
If it exists, type this at run dialog:
regsvr32 "path\stream_file_here.dll"
Chances are that the steam component file has been unregistered in the registry and you can't create an object of that.

Resources