Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Editing various file types

Posted on 2002-05-01
Medium Priority
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!
Question by:neuron1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
LVL 18

Accepted Solution

deighton earned 300 total points
ID: 6983469
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

Expert Comment

ID: 6983472
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

Expert Comment

ID: 6983620
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 16

Expert Comment

ID: 6983711

Expert Comment

ID: 6983767
LVL 13

Expert Comment

ID: 6983780
loitering ~:-]
LVL 22

Expert Comment

ID: 6983962
"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!

Author Comment

ID: 6984002
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... :-)

Author Comment

ID: 6984073
Checked and verified - ASCII it is.
Tnx to you all
LVL 22

Expert Comment

ID: 6984127

 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.

Expert Comment

ID: 6984725

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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

609 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