Solved

Editing various file types

Posted on 2002-05-01
11
155 Views
Last Modified: 2010-05-02
Hello all,
I need my function to be able to handle various files (i.e. Text, Office documents, Executables, etc.).

The function needs to open the file, edit some things in it, and write the changes back into it. I've tried doing this using FSO, but it only supports pure Text files...

How can I access the files and read information (char by char) from them, with no regard to their content and type? (which is irrelevant and unknown to me)


Thanks a lot!
0
Comment
Question by:neuron1
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
Comment Utility
Private Sub Form_Load()

    Dim b() As Byte
    Dim i As Integer
   
    i = FreeFile
   
    Open "c:\test\install.exe" For Binary Access Read Write As #i
   
    ReDim b(LOF(i) - 1)
    Get i, , b
   
    'change the byte array as appropriate
    b(0) = 46
   
    Put i, 1, b
   
    Close i
   
   

End Sub
0
 
LVL 2

Expert Comment

by:craigewens
Comment Utility
I asked a similar question once and the following was sent to me, i didn't use it but i think it suits your needs more.


'========bas module code==========
Const MAX_PATH = 260
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_DIRECTORY = &H10

Type FILETIME
 dwLowDateTime As Long
 dwHighDateTime As Long
End Type

Type WIN32_FIND_DATA
 dwFileAttributes As Long
 ftCreationTime As FILETIME
 ftLastAccessTime As FILETIME
 ftLastWriteTime As FILETIME
 nFileSizeHigh As Long
 nFileSizeLow As Long
 dwReserved0 As Long
 dwReserved1 As Long
 cFileName As String * MAX_PATH
 cAlternate As String * 14
End Type

Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData
As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData
As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Public Function ReplaceFileString(ByVal sFind As String, ByVal sReplace As String, ByVal sFileName As
String, Optional sFolder As String = "c:\") As Boolean
  If Len(sFind) <> Len(sReplace) Then
     MsgBox "You can replace string with same length only!", vbExclamation, "Error"
     Exit Function
  End If
  Dim sPath As String
  sPath = LocateFile(sFileName, sFolder)
  If sPath = "" Then
     MsgBox "File not found!", vbExclamation, "Error"
     Exit Function
  End If
  Dim nFile As Integer
  Dim sFileCont As String
  Dim abArray() As Byte
  nFile = FreeFile
  Open sPath For Binary As #nFile
       ReDim abArray(LOF(nFile) - 1)
       Get #nFile, , abArray
  Close #nFile
  Dim nPos As Long
  sFileCont = StrConv(abArray, vbUnicode)
  nPos = InStr(1, sFileCont, sFind)
  If nPos = 0 Then
     MsgBox "Search string not found!", vbExclamation, "Error"
     Exit Function
  End If
  Mid(sFileCont, nPos, Len(sReplace)) = sReplace
  abArray = StrConv(sFileCont, vbFromUnicode)
  Open sPath For Binary As #nFile
       Put #nFile, , abArray
  Close #nFile
  ReplaceFileString = True
End Function

Private Function LocateFile(sFileName As String, sFolder As String) As String
 Dim sTemp As String
 Dim lRet As Long, WFD As WIN32_FIND_DATA
 Dim hFile As Long, n As Integer
 Static bFound As Boolean
 Static sFound As String
 If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
 hFile = FindFirstFile(sFolder & sFileName, WFD)
 If hFile <> INVALID_HANDLE_VALUE Then
    sFound = sFolder & sFileName
    lRet = FindClose(hFile)
    bFound = True
    LocateFile = sFound
    Exit Function
 End If
 hFile = FindFirstFile(sFolder & "*.*", WFD)
 If hFile = INVALID_HANDLE_VALUE Then Exit Function
 sTemp = TrimNulls(WFD.cFileName)
 Do While sTemp <> ""
    If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
       If sTemp <> "." And sTemp <> ".." Then
          If Right$(sTemp, 1) <> "\" Then sTemp = sTemp & "\"
          Call LocateFile(sFileName, sFolder & sTemp)
       End If
    End If
    If bFound Then Exit Do
    lRet = FindNextFile(hFile, WFD)
    sTemp = ""
    If lRet <> 0 Then sTemp = TrimNulls(WFD.cFileName)
 Loop
 lRet = FindClose(hFile)
 If LocateFile = "" Then LocateFile = sFound
End Function

Private Function TrimNulls(sTemp As String) As String
 Dim l As Long
 l = InStr(1, sTemp, Chr(0))
 If l = 1 Then
    TrimNulls = ""
 ElseIf l > 0 Then
    TrimNulls = Left$(sTemp, l - 1)
 Else
    TrimNulls = sTemp
 End If
End Function

'=======using (Form code)========
Private Sub Command1_Click()
 If ReplaceFileString(Chr(&H10) & Chr(&H11), Chr(&H20) & Chr(&H20) , "myexe.exe", "d:\") Then
    MsgBox "Replacement successfull!"
 End If
'or
'  If ReplaceFileString("abcd", "edfg" , "myexe.exe", "d:\") Then
'     MsgBox "Replacement successfull!"
'  End If
End Sub
0
 
LVL 2

Expert Comment

by:dmarco
Comment Utility
listening...
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
:|
0
 
LVL 7

Expert Comment

by:jaynee
Comment Utility
lurking...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:crazyman
Comment Utility
loitering ~:-]
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
"How can I access the files and read information (char by char) from them, with no regard to their content and type"

I think you may be confused here (or I'm misunderstanding the question.)  If you're asking how to manipulate a file byte-by-byte, deighton has the answer.  If you're asking how to modify the human-interpreted version of the file, this is sort of like asking someone to modify a WWII German encoded message...you simply can't do it without decrypting the message.

MS Word has one form of encoding.  MS Excel has a different form of encoding, GIF files have yet a different form of encoding.

In order to perform a valid modification to a file, you have to convert it into a modifiable format, change it, then convert it back into that format.  That only works with a converter, and that will only work by knowing what format the file is in.

In the case of FSO, it reads text-formatted files and writes back text-formatted files.  In the case of Photoshop, it reads and writes GIF-formatted files.

So to answer your question as I understand it, you cannot read a file's viewable content without knowing its type.

--
A further hitch in all this is that years ago, IBM had a competing text format called EBCDIC, and if you tried to read that into an ASCII reader, you'd get gibberish.  The reason is because the formats of the text files were different.  To overcome that, you had to use the proper text converter!
0
 

Author Comment

by:neuron1
Comment Utility
=======================================
To deighton:
=============
First of all - thanks, it looks like what I need.
Now for the refinement;

In the code you've provided you put the file content into an array of bytes. Can I later handle that data in that array's cells as ASCII codes?
In what format does the array keep the file data? (ASCII, HEX, ..)

Reminder, I need it in ASCII.

=======================================
To craigewens:
=============
From a quick browse it seems just what deighton said :-)

=======================================
To rspahitz:
=============
The phrasing might have lacked some sence but I'm not that confused... :-)
0
 

Author Comment

by:neuron1
Comment Utility
Checked and verified - ASCII it is.
Tnx to you all
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
FYI:

 ASCII is a code (as in encoding method)
 Hex is a display format as is decimal

Any ASCII code element can be displayed in Hex, Decimal, Binary, etc.
0
 
LVL 2

Expert Comment

by:craigewens
Comment Utility
Neuron1,

Yes deighton's example is the basics behind the code i demonstraighted, it seems we created our answers at the same time which is why it seems i posted the same as him/her.

Either way i hope you make the code work for yourself.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now