The function from the VB 6 code is this (watch for word wrap):
Public Function GetMenuData(ByVal strTableName As String, nOffset As
Integer, nLen As Integer) As String
On Error Resume Next
Dim strTableData As String
Dim oFs As Scripting.FileSystemObject
Set oFs = CreateObject("Scripting.filesystemobject")
If oFs.FileExists(strTableName) Then
Dim hFile As Integer
hFile = FreeFile
Open strTableName For Binary Access Read As #hFile
strTableData = String$(nLen, " ") '&H0)
Get #hFile, nOffset, strTableData
Close #hFile
Else
g_oErr.AddMessage MODULE_NAME, "GetMenuData", "Missing " &
strTableName, App
Debug.Print "Table " & strTableName & " is missing."
'This will force the application to stop
Debug.Assert (1 = 2)
End If
Set oFs = Nothing
GetMenuData = strTableData
End Function
Not having worked with binary files, I did some research and found
some code from the internet which seemed to do what I wanted. I made
a couple of modifications. The new VB Script code is this:
Function GetMenuData(strTableName,nOffset, nLen)
Dim I, S, oInFile
Dim oFs, Binary
Set oFs = CreateObject("Scripting.filesystemobject")
If oFs.FileExists(strTableName) Then
Set oInFile = oFS.OpenTextFile(strTableName, 1)
Binary = oInfile.ReadAll
For I = nOffset To nlen
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
msgbox S
GetMenuData = S
End If
End Function
The new code doesn't return anything. I know the binary files have
data in them. Is there anything wrong with the script? Also, is
there an easy way to see what is in the binary file?
Thanks,
Jennifer
The code that you wrote is almost OK, but there
are quirks in the FSO that you have to deal with.
The designers of WSH apparently thought that no scripters
would have any reason to access binary files, so the
FSO is especially designed to access only text files. Of course,
all files are binary files, but since the FSO is designed to
assume that all files are text, it can act up in some situations
if you give it a non-text file.
For anyone in the US, Europe, Australia or other
Western-language country, an ANSI text file and a binary file are
a byte-for-byte match, so you can process a binary file
with Chr, Asc, etc. In other words, each character is a byte,
so you can alternate between characters and bytes. But when
you read it in you have to first get the file length and then
use Read(fileLength) to read in the file. The reason for that is
one of the FSO quirks: VBS sees a Chr(0) as the end of a text
string. When you use ReadAll it reads only until it finds a Chr(0).
ReadAll doesn't have the intelligence to check the file length first.
It assumes you're only dealing with text files. But the Textstream
object doesn't have any trouble reading a string with Chr(0) in it
if you're careful in how you do it.
See here for a class that includes all functions
necessary to handle binary files:
http://www.jsware.net/jsware/scripts.phi#bints
----------------------------------
Also, if you're writing VB6 it would be a good idea to keep
the FSO out of it. It's not available on all systems. It's
slow and clunky. And as you can see from the problem
you're having now, it's very "dumb" in the sense that the
designers made a lot of assumptions about it being used
in a very limited way. Most of the FSO functions are similar
to the Textstream in that the underlying code is very simplistic.
(Functions like GetBaseName, GetExtensionName, etc. are
no more than simple string chopping operations that you can
do yourself.)
For a basic FileExists in VB you can use something like
the following, which is actually what Microsoft uses in the
code they wrote for the Package and Deployment Wizard
Setup1.exe:
-----------------------------
Public Function FileExists(ByVal sPath As String) As Boolean
Dim i As Integer
On Error Resume Next
Err.Clear
FileExists = False
i = GetAttr(sPath)
If (Err = 0) Then
If (i And vbDirectory) = 0 Then
FileExists = True
End If
End If
Err.Clear
End Function
----------------------------
-----------
intFileNum = FreeFile
Open strPathName For Input As intFileNum
FileExists = (Err.Number = 0)
-----------
They used the GetAttr method to check for a folder
existing:
---------------
Public Function DirExists(ByVal strDirName As String) As Boolean
On Error Resume Next
DirExists = (GetAttr(strDirName) And vbDirectory) = vbDirectory
Err.Clear
End Function
----------------
In terms of VB6 and the FSO, I think that at least one VB6 MVP has
written a class to replace the FSO, so that you could virtually just add
the class to a project and then swap out your FSO references
for the class reference. That would yield faster, more dependable VB
code with no dependency on SCRRUN.DLL.
If you're curious about the MS functions from the
Package and Deployment Wizard, see the Setup1 project
in the Wizards\PDWizard folder. Setup1.exe is a VB
project and the whole thing is there.
Your link at: http://www.jsware.net/jsware/scripts.phi#bints is no good;
Also, rather than showing an example of determining whether or not a file
exists, could you rather modify the OP's code to run as requested?
I am interested in alternative approaches as well and would be highly
interested in seeing functional code using the OP's model. I'm in no danger
of having my in-house code run by the Chinese...so I think I'm geographically
safe... : )
Thank you!
Jennifer
On May 9, 9:51 am, "mayayana" <mayaXXyan...@mindXXspring.com> wrote:
> Some people use ADODB. You might want to
> look that up. But I don't think it's available on all systems
> and is blocked on some systems due to security issues.
>
> The code that you wrote is almost OK, but there
> are quirks in the FSO that you have to deal with.
> The designers of WSH apparently thought that no scripters
> would have any reason to accessbinaryfiles, so the
> FSO is especially designed to access only text files. Of course,
> all files arebinaryfiles, but since the FSO is designed to
> assume that all files are text, it can act up in some situations
> if you give it a non-textfile.
>
> For anyone in the US, Europe, Australia or other
> Western-language country, an ANSI textfileand abinaryfileare
> a byte-for-byte match, so you can process abinaryfile
> with Chr, Asc, etc. In other words, each character is a byte,
> so you can alternate between characters and bytes. But when
> youreadit in you have to first get thefilelength and then
> useRead(fileLength) toreadin thefile. The reason for that is
> one of the FSO quirks: VBS sees a Chr(0) as the end of a text
> string. When you use ReadAll it reads only until it finds a Chr(0).
> ReadAll doesn't have the intelligence to check thefilelength first.
> It assumes you're only dealing with text files. But the Textstream
> object doesn't have any trouble reading a string with Chr(0) in it
> if you're careful in how you do it.
>
> See here for a class that includes all functions
> necessary to handlebinaryfiles:http://www.jsware.net/jsware/scripts.phi#bints
> > Open strTableName ForBinaryAccessReadAs #hFile
> > strTableData = String$(nLen, " ") '&H0)
> > Get #hFile, nOffset, strTableData
> > Close #hFile
> > Else
> > g_oErr.AddMessage MODULE_NAME, "GetMenuData", "Missing " &
> > strTableName, App
> > Debug.Print "Table " & strTableName & " is missing."
> > 'This will force the application to stop
> > Debug.Assert (1 = 2)
> > End If
> > Set oFs = Nothing
> > GetMenuData = strTableData
> > End Function
>
> > Not having worked withbinaryfiles, I did some research and found
> > some code from the internet which seemed to do what I wanted. I made
> > a couple of modifications. The new VB Script code is this:
>
> > Function GetMenuData(strTableName,nOffset, nLen)
> > Dim I, S, oInFile
> > Dim oFs,Binary
>
> > Set oFs = CreateObject("Scripting.filesystemobject")
> > If oFs.FileExists(strTableName) Then
> > Set oInFile = oFS.OpenTextFile(strTableName, 1)
> > Binary= oInfile.ReadAll
> > For I = nOffset To nlen
> > S = S & Chr(AscB(MidB(Binary, I, 1)))
> > Next
> > msgbox S
> > GetMenuData = S
> > End If
> > End Function
>
> > The new code doesn't return anything. I know thebinaryfiles have
> > data in them. Is there anything wrong with the script? Also, is
> > there an easy way to see what is in thebinaryfile?
>
> > Thanks,
> > Jennifer- Hide quoted text -
>
> - Show quoted text -
The initial page to the link was bad in my browser; but it did give a
redirect that worked; my bad.
Interesting site; thanks for the links ---
So if you go to jsware.net with FDM installed you'll get a
403 (prohibited) response, even though you're actually
using IE, Firefox, etc.
If you're using VB6 you might also want
to write your own wrapper. There's one at jsware,
called "jsbin". I didn't mention it because I
figured that you probably wanted straight
script code, but a wrapper COM DLL will be
faster and easier to use if you don't need to
worry about widespread support.
I'd be happy to share the source code for
jsbin, as well. I don't make the project source code
for the components available publicly
as projects because they might end up getting
recompiled in multiple, incompatible versions.
But I don't mind sharing the basic code behind
them, in the form of .bas files, etc.
http://www.jsware.net/jsware/scripts.php3#bints
> Hello all. I am in the process of converting some VB 6 code to a VB
> Script. One of the things this code does is read in a binary file and
> then return text...
>
> The function from the VB 6 code is this (watch for word wrap):
>
> Public Function GetMenuData(ByVal strTableName As String, nOffset As
> Integer, nLen As Integer) As String
[..]
> Open strTableName For Binary Access Read As #hFile
[..]
> End Function
>
> Not having worked with binary files, I did some research and found
> some code from the internet which seemed to do what I wanted. I made
> a couple of modifications. The new VB Script code is this:
>
> Function GetMenuData(strTableName,nOffset, nLen)
[..]
> Set oFs = CreateObject("Scripting.filesystemobject")
[..]
> Set oInFile = oFS.OpenTextFile(strTableName, 1)
> Binary = oInfile.ReadAll
[..]
> End If
> End Function
>
> The new code doesn't return anything. I know the binary files have
> data in them. Is there anything wrong with the script? Also, is
> there an easy way to see what is in the binary file?
Hi,
like mayayana told you, there is an issue with
ReadAll vs. Read(bytes) due to the terminating effect of the
Ascii-Nullchar.
As for usage of FSO-*Text*Streams with binary files in general,
it's officially highly unrecommended, although it works most of
the time ;-)
Have a look at Eric Lipperts blog at MSDN:
"Binary Files and the File System Object Do Not Mix"
http://blogs.msdn.com/ericlippert/archive/2005/04/20/410127.aspx
Btw, Eric is one of the major architects of IIS, WSH and related
stuff.
MfG,
Alex
Try something like this:
=========
Function ReadBinaryFile(FileName)
Const adTypeBinary = 1
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFile FileName
ReadBinaryFile = BinaryStream.Read
BinaryStream.Close
Set BinaryStream = Nothing
End Function
==========
And Google up ADODB.Stream