Remplacer un mot dans un fichier texte
je voudrais remplacer le mot "remplace" dans un fichier test.vbs par une autre; via le code ci-dessous!
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Const ForReading = 1
Const ForWriting = 2
strFileName = "test.vbs"
strOldText = "remplace"
strNewText = "------"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText
objFile.Close |
ou celui -ci ne donne rien :
Code:
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
|
' http://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' ForReading 1 Open a file for reading only. You can't write to this file.
' ForWriting 2 Open a file for writing.
' ForAppending 8 Open a file and write to the end of the file.
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
' TristateUseDefault -2 Opens the file using the system default.
' TristateTrue -1 Opens the file as Unicode.
' TristateFalse 0 Opens the file as ASCII.
'Const ForReading = 1
'Const ForWriting = 2
Dim fso, AModif, Modif, Fichier1, fichier2
AModif = "remplace"
Modif = "-----------"
Fichier1 = "C:\tmp\!\test.vbs"
Fichier2 = "C:\tmp\!\test1.vbs"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set F1 = oFSO.OpenTextFile (Fichier1, ForReading)
Set F2 = oFSO.CreateTextFile (Fichier2, ForWriting)
Do Until F1.AtEndOfStream
Lit = F1.ReadLine
If Lit = AModif Then
F2.WriteLine Modif
Else
F2.WriteLine Lit
End If
Loop
Set F1 = Nothing
Set F2 = Nothing
oFSO.DeleteFile Fichier1
oFSO.MoveFile Fichier2, Fichier1 |
malgrés ces infos :
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
http://ss64.com/vb/syntax-replace.html
je galère depuis des heures!
merci d'avance de votre aide...