| Included are two functions that should make life easier for you. If you want to get part of the contents of a file or print a string to a file and you just plan to close the file as soon as you get through with it, then you'll probably like these. BAS Module Code 'Start Public Function GetFromFile(ByVal FileName As String, _ ByRef FileText As String, ByVal Start As Long) As Boolean FFile = FreeFile On Error GoTo NotAFile Open FileName For Input As FFile Close #FFile Open FileName For Binary As 1 If Start = -1 Then Seek #FFile, LOF(#FFile) + 1 Else Seek #FFile, Start End If Get #1, , FileText Close #FFile GetFromFile = True Exit Function NotAFile: GetFromFile = False Exit Function End Function Public Function PutToFile(ByVal FileName As String, _ ByVal FileText As String, ByVal Start As Long) As Boolean FFile = FreeFile On Error GoTo NotAFile Open FileName For Binary As 1 Seek #FFile, Start Put #1, , FileText Close #FFile PutToFile = True Exit Function NotAFile: PutToFile = False Exit Function End Function 'Finish Form Code None. Extra Details Syntax: RetVal = GetFromFile(FileName, FileText, Start) Syntax: RetVal = PutToFile(FileName, FileText, Start) For GetFromFile: The first parameter is a file's name. The second parameter is the string that the file's contents get returned in. Note: The second parameter is passed ByRef, so it MUST be dimmed as a string! The third parameter is the point at which to begin getting from the file. Note: The number of bytes to return is determined by the length of FileText when it is passed. For PutToFile: The first parameter is a file's name. The second parameter is the string that will be put into the file. The third parameter is the point in the file at which to put the string. Note: To add to the end of the file, pass -1 for the starting point. If either the function fails for any reason, the return value is False. Otherwise, it is True. |
Get or Put Part of a File in 1 Line of Code |
Express News India | Freelance ecommerce web development India