Can anyone give me some examples of working with files in binary? I want to store a whole file to a varible.
thanks.
Printable View
Can anyone give me some examples of working with files in binary? I want to store a whole file to a varible.
thanks.
To get an entire file into a variable ;
VB Code:
Option Explicit Private Function GetFileContents(fileName As String) As String If Dir(fileName) <> "" Then Open fileName For Binary As #1 GetFileContents = Input(LOF(1), 1) Close #1 End If End Function Private Sub Form_Load() MsgBox GetFileContents("c:\autoexec.bat") End Sub
What about opening a new file and writing to it?
VB Code:
Open "c:\myFile.txt" For Binary As #1 Put #1, , "oiasjdosiajsod" Close #1
OK cheers.
Is it possible to open a .exe file, read its contents then write it somewhere else, As I have tried with that code and it dont work :(
it dont even work for .txt files!!!!
First off, a post regarding looping thru the files.
When opening files for Input, one can read data as follows :
VB Code:
Dim strBuff As String Open myFile For Input As #1 Do Until Eof(1) Line Input #1, strBuff Loop Close #1
But you dont do it that way with binary files.
You do this instead :
VB Code:
Dim strBuff As String Open myFile For Binary As #1 Do Until Loc(1) = Lof(1) strBuff = Input(numberOfBytesToRead, 1) 'now do something with your numberOfBytesToRead Bytes of data Loop Close #1
Then regarding reading in some data and putting it somewhere else :
VB Code:
Private Sub Form_Load() Open "c:\command.com" For Binary As #1 Open "c:\output.txt" For Binary As #2 Put #2, , Input(4096, 1) Close #2 Close #1 End Sub
It creates the file, but it dont run :(
Well of course it wont run, you're only loading in 4096 bytes of command.com
Damn I forgot about that, but the output file is say its 40KB, same as the EXE.
how do i get the size of the file?
Well if you use
VB Code:
Input(4096, 1)
Then you're only reading in 4Kb.
Anyway, to get the size of a file open in binary mode use Lof(n).
I know 4096 is 4Kb :pQuote:
Originally posted by plenderj
Well if you use
VB Code:
Input(4096, 1)
Then you're only reading in 4Kb.
Anyway, to get the size of a file open in binary mode use Lof(n).
I need a little more help with the Lof(n) thingy, Im crap with binary :(
VB Code:
Open "c:\command.com" For Binary As #1 MsgBox "Size of file : " & Lof(1) Close #1
this is the code that im testing it on
[/Highlight]
Sub bMoveFile(OldLoc, NewLoc)
Dim byteSize
Open OldLoc For Binary As #1
byteSize = LOF(1)
Close #1
Open OldLoc For Binary As #1
Open NewLoc For Binary As #2
Put #2, , Input(byteSize, 1)
Close #2
Close #1
End Sub
[Highlight=VB]
it dont work, my EXEs wont run, and when i do a txt file i look and the new file has funny chars at the beginning.
Or you could juse use the filecopy statement.
VB Code:
Option Explicit Sub bMoveFile(OldLoc As String, NewLoc As String) Open OldLoc For Binary As #1 Open NewLoc For Output As #2 Print #2, Input(LOF(1), 1) Close #2 Close #1 End Sub Private Sub Form_Load() bMoveFile "c:\windows\explorer.exe", "c:\myExplorer.exe" End Sub
This isnt really for moving files, I want to save them into a varible, and maybe write them somewhere if they are needed.
I tried that code with the LOF(1) in the statment itself first, and that didnt work either :(
does that code work for you? (on a .exe)
Chenko, here another way to clone any filetype you like. Hope it help
VB Code:
Dim buff As String Open "C:\Crsyb14.hlp" For Binary As #1 buff = String(LOF(1), Chr(0)) Get #1, 1, buff Close #1 Open "C:\Crsyb15.hlp" For Binary As #1 Put #1, 1, buff Close #1
regards
Oh yeah, forgot about that :eek:
...and he gets it in one, LOL :pQuote:
Originally posted by plenderj
Oh yeah, forgot about that :eek:
thanks both :)
Well my piece of code just above works !
well didnt work for me :p
Also, is there a way I can store that varible in my project when I compile, like a const etc...?
Well worked for me :p
Anyway if you wanted to store it as a const, you could just take the string value that is the file, and then just put into the src :
VB Code:
Public Const appString As String = "...."
However
The binary data would contain control characters that would **** up the source code. So you would probably have to Base64 encode it before storing it in your src.
Yea, a lot of "dfweioan" & _ :(
How do I Base64 Encode, I will probaly save each file away in a seperate module anyway :)
Well the idea with Base64 encoding is that it removes all the funny characters from strings. You then Base54 decode the string into its original format.
Its usually how attachments are put into emails.
PlainText <> Binary
Anyway there's lots of Base64 encoder functions on the net.
Ah ok, Looks like me gonna do some searchin' :rolleyes:
cheers anyways
I like the FileSytstemObject and the Textstream object
:DCode:Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
i used this... using the "DecodeFile" fucntion
VB Code:
Option Explicit Private Type b64encoded Byte1 As Byte Byte2 As Byte Byte3 As Byte Byte4 As Byte End Type Private Type b64decoded Byte1 As Byte Byte2 As Byte Byte3 As Byte End Type Private Type codecodeBytes Byte1 As Byte Byte2 As Byte Byte3 As Byte Byte4 As Byte End Type Dim keyByteA As codecodeBytes Dim keyByteB As codecodeBytes Dim keyByteC As codecodeBytes Private Sub InitDecodeEncodeMachine() '------------------------------- keyByteA.Byte1 = &H3F keyByteA.Byte2 = &H4 keyByteA.Byte3 = &H30 keyByteA.Byte4 = &H10 '------------------------------- '------------------------------- keyByteB.Byte1 = &HF keyByteB.Byte2 = &H10 keyByteB.Byte3 = &H3C keyByteB.Byte4 = &H4 '------------------------------- '------------------------------- keyByteC.Byte1 = &H3 keyByteC.Byte2 = &H40 keyByteC.Byte3 = &H3F keyByteC.Byte4 = &H1 '------------------------------- End Sub 'Decode source file encoded by base64 in ' to destination Public Sub DecodeFile(ByVal srcFile As String, ByVal dstFile As String) Dim tempBuffer As String * 78 Dim tempBufferNC As String * 74 Dim tempEncoded As b64encoded Dim tempDecoded As b64decoded Dim bResult As Byte Dim iCntr As Long Dim btResult As Byte Call InitDecodeEncodeMachine btResult = 0 iCntr = 0 Open srcFile For Random As #1 Len = 78 Open dstFile For Random As #2 Len = 1 Do While Not (EOF(1)) Get #1, , tempBuffer iCntr = 0 Do While iCntr < Len(tempBuffer) If Mid(tempBuffer, (iCntr + 1), 2) = vbCrLf Then Exit Do tempEncoded.Byte1 = DeMapCode(Mid(tempBuffer, (iCntr + 1), 1)) tempEncoded.Byte2 = DeMapCode(Mid(tempBuffer, (iCntr + 2), 1)) tempEncoded.Byte3 = DeMapCode(Mid(tempBuffer, (iCntr + 3), 1)) tempEncoded.Byte4 = DeMapCode(Mid(tempBuffer, (iCntr + 4), 1)) bResult = 0 bResult = Base64Decode(tempEncoded, tempDecoded) Select Case bResult Case 1 Put #2, , tempDecoded.Byte1 Case 2 Put #2, , tempDecoded.Byte1 Put #2, , tempDecoded.Byte2 Case 3 Put #2, , tempDecoded.Byte1 Put #2, , tempDecoded.Byte2 Put #2, , tempDecoded.Byte3 End Select 'EOF encoded part If (bResult = 0) Then Exit Do 'FOUR bytes as step iCntr = iCntr + 4 Loop 'if end of encoded text If (bResult = 0) Then Exit Do Loop Close #2 Close #1 End Sub Private Function Base64Decode(srcBase64Encoded As b64encoded, dstBase64Decoded As b64decoded) As Byte 'return amoun of decoded bytes If (srcBase64Encoded.Byte1 > 64) Then Base64Decode = 0 Exit Function End If If ((srcBase64Encoded.Byte3 = 64) And (srcBase64Encoded.Byte4 = 64)) Then dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _ (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4 dstBase64Decoded.Byte2 = 0 dstBase64Decoded.Byte3 = 0 Base64Decode = 1 Exit Function End If If (srcBase64Encoded.Byte4 = 64) Then dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _ (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4 dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _ (srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4 dstBase64Decoded.Byte3 = 0 Base64Decode = 2 Exit Function End If dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _ (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4 dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _ (srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4 dstBase64Decoded.Byte3 = (srcBase64Encoded.Byte3 And keyByteC.Byte1) * keyByteC.Byte2 + _ (srcBase64Encoded.Byte4 And keyByteC.Byte3) / keyByteC.Byte4 Base64Decode = 3 End Function Private Function DeMapCode(srcChar As String) As Byte If Len(srcChar) <> 1 Then DeMapCode = 0 Exit Function End If Select Case srcChar Case "A" To "Z" DeMapCode = Asc(srcChar) - 65 Case "a" To "z" DeMapCode = Asc(srcChar) - 97 + 26 Case "0" To "9" DeMapCode = Asc(srcChar) - 48 + 52 Case "+" DeMapCode = 62 Case "/" DeMapCode = 63 Case "=" DeMapCode = 64 Case Else DeMapCode = 65 End Select End Function
...and it returned this in a file "1A" thats all!!
I would suggest trying a few different ones.
And then test by doing something like this :
VB Code:
Dim i As Long, j As Long Dim myString As String Dim worksOk As Boolean Randomize worksOk = True For i = 0 To 1000 For j = 0 To 100 myString = myString & Rnd Next j worksOk = worksOk And (myString = Base64Decode(Base64Encode(myString))) myString = "" Next i MsgBox "Test Successul : " & worksOk
You'd probably have to change it around a little for different versions of the function.
http://www.vbforums.com/showthread.p...threadid=93732
from RyeBread, works sweet.
Well assuming you've tested it and all is well, then you can start converting binary data into plaintext that you can copy and paste straight into your source code.
The strings will be about 100,000 chars long!! LOL
I made a program to split the lines up, and i made continuation on them, as you can only have 10 conncurrent... first try and it didnt work :(
Can be arsed at the moment anymore.
Jesus4u: The FSO is only good for ASCII text files, not binary.