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 64 65
|
Private Function ChangeWords(sWordsToRemove, sWordsToChange, sFile)
If FileExists(sFile) Then
' ouvre le fichier
Dim sBuffer
sBuffer = ReadDataFromFile(sFile)
' ligne à changer existe?
If InStr(1, sBuffer, sWordsToRemove) > 0 Then
sBuffer = Replace(sBuffer, sWordsToRemove, sWordsToChange)
WriteDataToFile sFile, sBuffer
ChangeWords = True
End If
End Function
Function ReadDataFromFile(sPathFile)
' reader
Dim oFSO
Set oFSO = CreateObject("Scripting.fileSystemObject")
' fichier
Dim oFile
Set oFile = oFSO.GetFile(sPathFile)
' stream
Dim oStream
Set oStream = oFile.OpenAsTextStream(ForReading)
' retour
ReadDataFromFile = oStream.ReadAll
' nettoyage
oStream.Close
Set oStream = Nothing
Set oFile = Nothing
Set oFSO = Nothing
End Function
Function FileExists(sPathFile)
' reader
Dim oFSO
Set oFSO = CreateObject("Scripting.fileSystemObject")
' retour
FileExists = oFSO.FileExists(sPathFile)
' nettoyage
Set oFSO = Nothing
End Function
Sub WriteDataToFile(sPathFile, sData)
' reader
Dim oFSO
Set oFSO = CreateObject("Scripting.fileSystemObject")
' fichier : chemin, 2 pour écrire (et écraser), True pour forcer la création du fichier
Dim oFile
Set oFile = oFSO.openTextFile(sPathFile, 2, True)
' écriture
oFile.Write sData
' nettoyage
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing
End Sub
'UTILISATION :
WriteDataToFile "c:\machin.txt", "mon texte à écrire" |
Partager