-
Feb 24th, 2012, 03:38 AM
#1
Thread Starter
Don't Panic!
Problems reading a file API
Hi,
I always seem to get errors with the APIs... sigh.
Right, yesterday code like this worked fine. Then I got a corrupted mdb with the code in and had to re-write it to the below.
The code below doesn't read the file. It's erroring with last dll error 5 (access denied) which is very weird as I have a segment of test code that I copied this from and that works.
So I must have missed something that is holding that file open.
Any ideas?
Code is to read byte by byte, find the vblf that doesn't have a vbcr before it and insert the vbcr, then output to a new file with the original name. Due to my vba code later to read the file and it failing due to no vbcrlf - original text file from a linux system I think...
Code:
strFNSrc = strFN & "_Orig.txt"
Name strFN As strFNSrc
lngFH_Dest = apiFileCreate(strFN, FileDA_Generic_Read, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
lngFH_Src = apiFileCreate(strFNSrc, FileDA_Generic_Write, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
If lngFH_Src <> -1 And lngFH_Dest <> -1 Then
lngBytesToRead = 4096
lngBytesRed = lngBytesToRead
'dblFileSize = GetTheFileSizeAPI(lngFH_Src)
Do Until lngBytesRed < lngBytesToRead
ReDim aryBuffer(lngBytesToRead)
ReDim aryNewBuffer(lngBytesToRead + 512)
lngRes = apiFileRead(lngFH_Src, aryBuffer(0), lngBytesToRead, lngBytesRed, 0)
If lngRes = 0 Then Debug.Print Err.LastDllError
lngBytesToWrite = 0
If lngBytesRed > 0 Then
For lngL = 0 To lngBytesRed - 1
If aryBuffer(lngL) = 10 And Not blnPrevCR Then
aryNewBuffer(lngBytesToWrite) = 13
lngBytesToWrite = lngBytesToWrite + 1
aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
Else
aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
End If
lngBytesToWrite = lngBytesToWrite + 1
Next
Else
txtProgress = Format(Now, "hh:nn:ss") & " - Failed to copy data file" & vbCrLf & txtProgress
End If
lngRes = apiFileWrite(lngFH_Dest, aryNewBuffer(0), lngBytesToWrite, lngBytesWritten, 0)
'---- store how much has been processed
dblFileDone = dblFileDone + lngBytesRed
PercBar boxPerc, txtProgress, dblFileDone, dblFileSize
Loop
lngRes = apiCloseHandle(lngFH_Src)
lngRes = apiCloseHandle(lngFH_Dest)
End If
End If
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Feb 24th, 2012, 03:51 AM
#2
Thread Starter
Don't Panic!
Re: Problems reading a file API
Just to add, the file I am copying and the code that I copied this from will read the file when asked to. It just doesn't here in this code.
What am I missing?
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Feb 24th, 2012, 05:49 AM
#3
Thread Starter
Don't Panic!
Re: Problems reading a file API
This works... when in a module and called via immediates window.
Code:
Public Sub PreScanFile(ByVal strFileName As String, ByVal blnEraseCopy As Boolean)
Dim strFN As String
Dim strFNOrig As String
Dim lngRes As Long
Dim lngFHSrc As Long
Dim lngBytesToRead As Long
Dim lngBytesRed As Long
Dim aryBuffer() As Byte
Dim lngFHDest As Long
Dim lngBytesToWrite As Long
Dim lngBytesWritten As Long
Dim aryNewBuffer() As Byte
Dim lngL As Long
Dim blnPrevCR As Boolean
strFN = strFileName
strFNOrig = strFileName & "Orig.txt"
Name strFN As strFNOrig
lngFHSrc = apiFileCreate(strFNOrig, FileDA_Generic_Read, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
lngFHDest = apiFileCreate(strFN, FileDA_Generic_Write, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
If (lngFHSrc <> -1) And (lngFHDest <> -1) Then
Debug.Print "Source : " & lngFHSrc
Debug.Print "Dest : " & lngFHDest
lngBytesToRead = 4096
ReDim aryBuffer(lngBytesToRead)
ReDim aryNewBuffer(lngBytesToRead + 512)
lngBytesRed = lngBytesToRead
blnPrevCR = False
Do Until lngBytesRed < lngBytesToRead
lngRes = apiFileRead(lngFHSrc, aryBuffer(0), lngBytesToRead, lngBytesRed, 0)
If lngBytesRed > 0 Then
lngBytesToWrite = 0
For lngL = 0 To lngBytesRed - 1
If aryBuffer(lngL) = 10 And Not blnPrevCR Then
aryNewBuffer(lngBytesToWrite) = 13
lngBytesToWrite = lngBytesToWrite + 1
End If
aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
lngBytesToWrite = lngBytesToWrite + 1
blnPrevCR = aryBuffer(lngL) = 13 'vbcr
Next
lngRes = apiFileWrite(lngFHDest, aryNewBuffer(0), lngBytesToWrite, lngBytesWritten, 0)
End If
Loop
lngRes = apiCloseHandle(lngFHDest)
lngRes = apiCloseHandle(lngFHSrc)
End If
If blnEraseCopy Then Kill strFNOrig
End Sub
No idea why this works and the other doesn't...
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|