Le problème du ForAppending c'est qu'il ajoute à la fin du txt
Dim Num, Compt As Integer Dim Texte As String Dim Lignes(100) As String '100 lignes de texte au max 'On lit le fichier: Open "c:\users\carlos\desktop\TEXTE.txt" For Input As #1 Do While Not EOF(1) Num = Num + 1 Line Input #1, Texte Lignes(Num) = Texte Loop Close 'On efface le fichier Kill "c:\users\carlos\desktop\TEXTE.txt" 'On écrit le tout avec le 'bonjour' au début tout en créant le fichier Open "c:\users\carlos\desktop\TEXTE.txt" For Append As #1 Print #1, "Bonjour," For Compt = 1 To Num Print #1, Lignes(Compt) Next Compt Close
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDim Position_du_Mot_Rech As Integer Dim Texte_Lu, Texte_a_Insérer, Mot_Recherché As String Dim Fichier_a_Lire, Fichier_Modifié As String Fichier_a_Lire = "c:\users\carlos\desktop\TEXTE.txt" Fichier_Modifié = "c:\users\carlos\desktop\TEXTE_NEW.txt" 'Textes de ton choix: Texte_a_Insérer = " [Texte inséré] " Mot_Recherché = "demain" 'Mot à la suite duquel le texte à insérer sera inséré 'on crée le nouveau fichier en mode écriture (le #1): Open Fichier_Modifié For Append As #1 'On ouvre le fichier original pour lecture (le #2): Open Fichier_a_Lire For Input As #2 Do While Not EOF(2) Line Input #2, Texte_Lu 'on lit la ligne Position_du_Mot_Rech = InStr(Texte_Lu, Mot_Recherché) - 1 If Position_du_Mot_Rech > 0 Then 'Si le mot recherché existe dans le bloc texte: 'on écrit ce qui est à gauche du mot recherché, 'le mot recherché, 'le texte à insérer, 'et ce qui est à droite du mot recherché Print #1, Left(Texte_Lu, Position_du_Mot_Rech) & _ Mot_Recherché & Texte_a_Insérer & _ Right(Texte_Lu, (Len(Texte_Lu) - 1 - (Position_du_Mot_Rech + Len(Position_du_Mot_Rech) + 4))) Else Print #1, Texte_Lu 'Sinon il écrit la ligne normalement End If Loop Close 'on efface l'ancien fichier et on renome le nouveau Kill Fichier_a_Lire Name Fichier_Modifié As Fichier_a_Lire
Dim Position_DInsértion As Integer Dim Texte_Lu, Texte_a_Insérer, Fichier_a_Lire, Fichier_Modifié As String Fichier_a_Lire = "c:\users\carlos\desktop\TEXTE.txt" Fichier_Modifié = "c:\users\carlos\desktop\TEXTE_NEW.txt" 'Texte de ton choix: Texte_a_Insérer = " [Texte inséré] " 'Position où insérer le texte dans toutes les lignes (blocs de texte) '(Position 1 = premier caractère) Position_DInsértion = 3 'dans ce cas le texte sera inséré entre la 3° et la 4° lettre du texte 'on crée le nouveau fichier en mode écriture (le #1): Open Fichier_Modifié For Append As #1 'On ouvre le fichier original pour lecture (le #2): Open Fichier_a_Lire For Input As #2 Do While Not EOF(2) Line Input #2, Texte_Lu 'on lit la ligne 'on écrit ce qui est à gauche de la position d'insertion, 'le texte à insérer, 'et ce qui est à droite de la position d'insertion, Print #1, Left(Texte_Lu, Position_DInsértion) & _ Texte_a_Insérer & _ Right(Texte_Lu, (Len(Texte_Lu) - Position_DInsértion)) Loop Close 'on efface l'ancien fichier et on renome le nouveau Kill Fichier_a_Lire Name Fichier_Modifié As Fichier_a_Lire
Print #1, "load data infile *" _ & vbNewLine & "append into table h_cap_abs_ind" _ & vbNewLine & "fields terminated by ';'" _ & vbNewLine & "(" _ & vbNewLine & " CP_AE_AGENETAB," _ & vbNewLine & " CP_AB_CODABS," _ & vbNewLine & " CP_DATREF," _ & vbNewLine & " CP_NBJOURS," _ & vbNewLine & " CP_NBHRES," _ & vbNewLine & " CP_REGUL" _ & vbNewLine & ")" _ & vbNewLine & "BEGINDATA"
Print #1, "load data infile *" Print #1, "append into table h_cap_abs_ind" Print #1, "fields terminated by ';'" Print #1, "(" Print #1, " CP_AE_AGENETAB," Print #1, " CP_AB_CODABS," Print #1, " CP_DATREF," Print #1, " CP_NBJOURS," Print #1, " CP_NBHRES," Print #1, " CP_REGUL" Print #1, ")" Print #1, "BEGINDATA"
Dim Num, Compt As Integer Dim Lignes(600) As String '600 lignes de texte au max Dim Texte, txt_name As String Dim Texte_a_Insérer, Fichier_Modifié As String Num = 0 txt_name = CurrentProject.Path & "\rtt_a_integrer.txt" Fichier_Modifié = Left(txt_name, (Len(txt_name) - 4)) & "_new.txt" 'Générer le fichier RTT_principal DoCmd.TransferText acExportDelim, "RTT_A_GENERER Spécification d'exportation", "RTT_A_GENERER", txt_name, False, "" Texte_a_Insérer = "load data infile *" & vbCrLf & _ "append into table h_cap_abs_ind" & vbCrLf & _ "fields terminated by ';'" & vbCrLf & _ "(" & vbCrLf & _ " CP_AE_AGENETAB," & vbCrLf & _ " CP_AB_CODABS," & vbCrLf & _ " CP_DATREF," & vbCrLf & _ " CP_NBJOURS," & vbCrLf & _ " CP_NBHRES," & vbCrLf & _ " CP_REGUL" & vbCrLf & _ ")" & vbCrLf & _ "BEGINDATA" & vbCrLf 'on crée le nouveau fichier en mode écriture (le #1): Open Fichier_Modifié For Append As #1 'On ouvre le fichier original pour lecture (le #2): Open txt_name For Input As #2 'attention en mode 'Input' si le fichier n'existe pas une erreur apparait Do While Not EOF(2) Num = Num + 1 Line Input #2, Texte Lignes(Num) = Texte Loop Print #1, Texte_a_Insérer For Compt = 1 To Num Print #1, Lignes(Compt) Next Compt Close 'on efface l'ancien fichier et on renome le nouveau Kill txt_name 'on renome le nouvel fichier Name Fichier_Modifié As txt_name