Link to home
Start Free TrialLog in
Avatar of neuron1
neuron1

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of craigewens
craigewens

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
listening...
Avatar of Richie_Simonetti
:|
lurking...
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!
Avatar of neuron1

ASKER

=======================================
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... :-)
Avatar of neuron1

ASKER

Checked and verified - ASCII it is.
Tnx to you all
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.
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.