1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| Sub TestPourVoir()
Const F = "C:\Documents and Settings\argyronet\My Documents\TestLine1To64.txt"
If CleanFileToImport(F, 6, True) = False Then
MsgBox "Le fichier n'a pas pu être préparé :" & vbCrLf & vbCrLf & Error(m_lngError) & " (" & m_lngError & ")", vbExclamation
End If
End Sub
Private Function CleanFileToImport(ByVal ImportFilename As String, ByVal GetLinesAfter As Long, ByVal DeleteTempFileAfter As Boolean) As Boolean
Const TEMP_FILE_NAME As String = "VoidTmp~"
Dim oFSO As Scripting.FileSystemObject
Dim oStreamReader As Scripting.TextStream
Dim oStreamWriter As Scripting.TextStream
Dim strGoodLine As String
Dim strFileName As String
Dim strPathName As String
Dim strTempDataFile As String
Dim L As Long
On Error GoTo CleanFileToImport_Error
Set oFSO = New Scripting.FileSystemObject
strPathName = Left$(ImportFilename, InStrRev(ImportFilename, "\"))
strFileName = Mid$(ImportFilename, InStrRev(ImportFilename, "\") + 1)
strTempDataFile = strPathName & TEMP_FILE_NAME & strFileName
With oFSO
If .FileExists(strTempDataFile) Then .DeleteFile strTempDataFile, True
Set oStreamReader = .OpenTextFile(ImportFilename, ForReading, False)
Set oStreamWriter = .OpenTextFile(strTempDataFile, ForWriting, True)
End With
With oStreamReader
Do While .AtEndOfStream <> True
L = L + 1
strGoodLine = .ReadLine
If L > GetLinesAfter Then
oStreamWriter.WriteLine strGoodLine
End If
Loop
.Close
oStreamWriter.Close
End With
With oFSO
.CopyFile strTempDataFile, ImportFilename, True
If DeleteTempFileAfter Then
.DeleteFile strTempDataFile, True
End If
End With
CleanFileToImport = True
On Error GoTo 0
CleanFileToImport_Exit:
Set oStreamWriter = Nothing
Set oStreamReader = Nothing
Set oFSO = Nothing
Exit Function
CleanFileToImport_Error:
m_lngError = Err
CleanFileToImport = False
Resume CleanFileToImport_Exit
End Function |
Partager