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
| 'On Error Resume Next
Dim objUser
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshVarEnv = WshShell.Environment("PROCESS")
strNetBiosDomain = WshVarEnv.Item ("USERDOMAIN")
strLogonServer = WshShell.ExpandEnvironmentStrings("%Logonserver%")
strUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")
Set objUser = GetObject("WinNT://" & strNetBiosDomain & "/" _
& strUserName & ",user")
MsgBox strUserName
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists("e:\test") Then
Call searchReplaceFile("e:\test\toto.txt","bubul","totophe")
Else
' True : pour copier en "écrasant" destination si existe..
oFSO.CopyFolder "\\sw2k034\doc\test","e:\test",True
MsgBox ("fait")
Call searchReplaceFile("e:\test\toto.txt","tonton","totophe")
End If
WScript.Quit
function searchReplaceFile(File,LineSearch,LineReplace)
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
if oFSO.FileExists(File) = true Then
Dim oRegEx
Dim monFile
Dim sStream
Dim sNewStream
Set oRegEx = New RegExp
oRegEx.Global = True
oRegEx.Pattern = LineSearch
Set monFile = oFSO.OpenTextFile(File,1)
sStream = monFile.ReadAll
monFile.Close
sNewStream = oRegEx.Replace(sStream,LineReplace)
If InStr(sNewStream,LineReplace) = true Then
sNewStream = Replace(sNewStream,LineReplace,3,7)
end if
Set monFile = oFSO.OpenTextFile(File,2,true)
monFile.Write sNewStream
monFile.Close
MsgBox "Modification terminé",vbInformation,"Succès"
else
MsgBox "Le fichier source est introuvable",Vbcritical,"Erreur"
end If
end function |
Partager