Editing various file types

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!
Who is Participating?
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
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

 dwLowDateTime As Long
 dwHighDateTime As Long
End Type

 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
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData
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)
    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 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)
 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)
    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
'  If ReplaceFileString("abcd", "edfg" , "myexe.exe", "d:\") Then
'     MsgBox "Replacement successfull!"
'  End If
End Sub
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Richie_SimonettiIT OperationsCommented:
jayneeIT ManagerCommented:
loitering ~:-]
"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!
neuron1Author Commented:
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... :-)
neuron1Author Commented:
Checked and verified - ASCII it is.
Tnx to you all

 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.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.