Decoupe fichier par tranche d'enregistrements

khev Messages postés 4 Date d'inscription mercredi 27 avril 2005 Statut Membre Dernière intervention 21 avril 2009 - 21 avril 2009 à 08:49
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 21 avril 2009 à 09:10
Bonjour,

Je sollicite votre aide concernant le split d'un fichier. Au juste, je souhaite decouper une fichier par tranche de 20 enregistrements en alimentant les fichiers en fic1, fic2,......ficN. Exemple j'ai un fichier avec 41 enregistrements, je dois avoir 3 fichiers avec fic 1 20 fic 2 20 et fic 3 = 1
Merci par avance de votre aide.

1 réponse

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
21 avril 2009 à 09:10
Private Declare Function MkDir Lib "imagehlp.dll" Alias "MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Private Type PathType
Folder As String '# Inclue le '\' final
FileName As String
FileExt As String '# Inclue le '.'
End Type

'# Permet de découper un chemin en dossier / nom de fichier / extension
Private Function CrackPath(ByVal vsInput As String) As PathType
Dim nPos As Long
If LenB(vsInput) Then
nPos = InStrRev(vsInput, "")
If nPos Then
CrackPath.Folder = Left$(vsInput, nPos)
vsInput = Mid$(vsInput, nPos + 1)
End If

nPos = InStrRev(vsInput, ".")
If nPos Then
CrackPath.FileExt = Mid$(vsInput, nPos)
CrackPath.FileName = Left$(vsInput, nPos - 1)
Else
CrackPath.FileName = vsInput
End If
End If
End Function

Private Function SplitFile(ByRef vsInFilePath As String, ByRef vsOutFilePath As String, Optional ByVal vnRecordCount As Long = 20) As Long
Dim iFile As Integer
Dim xsLines() As String
Dim i As Long
If LenB(vsInFilePath) <> 0 And LenB(Dir$(vsInFilePath)) <> 0 And vnRecordCount > 0 Then
iFile = FreeFile
Open vsInFilePath For Input As #iFile
xsLines = Split(Input(LOF(iFile), iFile), vbNewLine)
Close iFile

For i = 0 To UBound(xsLines)
If (i Mod vnRecordCount) = 0 Then
If i Then
Close iFile
End If
iFile = FreeFile
With CrackPath(vsOutFilePath)
MkDir .Folder
Open .Folder & .FileName & "_" & (1 + i \ vnRecordCount) & .FileExt For Output As #iFile
End With
End If
Print #iFile, xsLines(i)
Next i
If i > 0 Then
Close #iFile
SplitFile = i
End If
End If
End Function

Private Sub Form_Load()
SplitFile "C:\Films.txt", "C:\Films\Liste.txt"
End Sub
0
Rejoignez-nous