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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| Option Explicit
Dim ws,Title,InPutFile,OutPutFile,Data
Dim dico,K,i,Mydate,Mois,Nom_des_mois,Num_Mois,Num,MyNewDate
Title = "Extraire une date depuis un fichier Texte by Hackoo"
If wscript.arguments.count > 0 Then
InPutFile = Wscript.Arguments(0)
If Not IsTXTFile(InPutFile) Then Call Display_Help_Usage()
OutPutFile = GetFilenameWithoutExtension(InPutFile) & "_Output.txt"
Data = ReadFile(InPutFile)
Call Main()
Set ws = CreateObject("wscript.shell")
'Explorer(OutPutFile)
ws.run "CMD /C Start /MAX Notepad " & chr(34) & OutPutFile & chr(34),0,True
Else
Call Display_Help_Usage()
End If
'-----------------------------------------------------------------
Sub Main()
Mydate = Extracting_Date(Data)
Mois = Split(MyDate," ")(1)
Set dico = CreateObject("Scripting.Dictionary")
Nom_des_mois = Array("janvier","fevrier","Mars","Avril","Mai","juin",_
"juillet","aout","septembre","octobre","novembre","decembre")
Num_Mois = Array("01","02","03","04","05","06","07","08","09","10","11","12")
For i=0 To UBound(Nom_des_mois)
dico(Nom_des_mois(i)) = Num_Mois(i)
Next
For Each K in dico
If K = Mois Then
Num = dico(K)
Exit For
End If
Next
MyNewDate = Replace(Mydate,Mois,Num)
MyNewDate = Replace(MyNewDate," ","/")
Call Write2File(MyNewDate,OutPutFile)
End Sub
'------------------------------------------------------------------
Sub Display_Help_Usage()
Dim ws
Set ws = CreateObject("wscript.shell")
ws.Popup "Vous devez faire glisser un fichier texte sur ce script" & vbCrLF &_
chr(34) & WSH.ScriptName & chr(34),8,Title,vbExclamation
Wscript.Quit(1)
End Sub
'------------------------------------------------------------------
Function IsTXTFile(sFile)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "(.txt)"
regEx.IgnoreCase = True
If regEx.Test(sFile) Then
IsTXTFile = True
End If
End Function
'------------------------------------------------------------------
Sub Explorer(File)
ws.run "Explorer /n,/select,"& File &"",1,True
End Sub
'------------------------------------------------------------------
Function GetFilenameWithoutExtension(FileName)
Dim Result, i
Result = FileName
i = InStrRev(FileName, ".")
If ( i > 0 ) Then
Result = Mid(FileName, 1, i - 1)
End If
GetFilenameWithoutExtension = Result
End Function
'------------------------------------------------------------------
Function ReadFile(InPutFile)
Dim objFSO,oTS,sText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = objFSO.OpenTextFile(InPutFile)
sText = oTS.ReadAll
oTS.close
set oTS = nothing
Set objFSO = nothing
ReadFile = sText
End Function
'------------------------------------------------------------------
Function Extracting_Date(Data)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = "\d{1,2} (\w+) \d{4}"
regEx.IgnoreCase = True
regEx.Global = True
If regEx.Test(Data) = False then
MsgBox "Pas de date trouvé",vbExclamation,Title
Wscript.Quit()
Else
Set Matches = regEx.Execute(Data)
For Each Match in Matches
Extracting_Date = Match.Value
Next
End If
End Function
'------------------------------------------------------------------
Sub Write2File(strText,OutPutFile)
Const ForWriting = 2
Dim fs,ts
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(OutPutFile,ForWriting,True)
ts.WriteLine strText
ts.Close
End Sub
'------------------------------------------------------------------ |
Partager