Page 1 sur 2
[R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 14:56
par Laddy
Bonjour
je cherche à extraire 3 informations différentes d'un fichier txt.
J'ai trouvé ce code de base dans un autre sujet que j'essaie d'adapter
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
While 1
$chars = FileReadLine($file)
If @error Then ExitLoop
If StringInStr($chars,"UUID",1) Then
$chars = stringsplit($chars,":")
$chars = $chars[2]
MsgBox(0, "Char read:", $chars)
EndIf
If StringInStr($chars,"Accessible",1) Then
$chars = stringsplit($chars,":")
$chars = $chars[2]
MsgBox(0, "Char read:", $chars)
EndIf
If StringInStr($chars,"Logical size",1) Then
$chars = stringsplit($chars,":")
$chars = $chars[2]
MsgBox(0, "Char read:", $chars)
EndIf
Wend
FileClose($file)
Le code me liste aussi cette ligne :
In use by VMs: XNGDOWS (UUID: ae7f4d38-bd27-4bf4-828f-b048a8a556a4)
comment la supprimer la supprimer du résultat ?
Et comment séparer chaque information pour afficher le résultat dans 3 inputbox ?
Sun VirtualBox Command Line Management Interface Version 3.1.6
(C) 2005-2010 Sun Microsystems, Inc.
All rights reserved.
UUID: 1182fa1e-c74c-45d0-97a5-7ab6b3a65946
Accessible: yes
Description:
Logical size: 10240 MBytes
Current size on disk: 6714 MBytes
Type: normal (base)
Storage format: VDI
In use by VMs: WINDOWS (UUID: ae7f4d38-bd27-4bf4-828f-b048a8a556a4)
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:18
par Tlem
Pourquoi faire compliqué ?
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
If StringLeft($Line, 5) = "UUID:" Then $String1 = $Line
If StringLeft($Line, 11) = "Accessible:" Then $String2 = $Line
If StringLeft($Line, 13) = "Logical size:" Then $String3 = $Line
WEnd
FileClose ($file)
MsgBox(0, '', 'Voici les information recherchées :' & @CRLF & @CRLF & _
$String1 & @CRLF & _
$String2 & @CRLF & _
$String3)
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:27
par Laddy
Merci Tlem
pourquoi faire compliquer ?
parce que je veux uniquement la partie après les : ?
et ton code me donne les 3 lignes entières.
Je suppose que je dois encore traiter les informations avec un String quelquechose.
Edit :
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
If StringLeft($Line, 5) = "UUID:" Then
$String1 = stringsplit($Line,":")
$String1 = $String1[2]
EndIf
If StringLeft($Line, 11) = "Accessible:" Then
$String2 = stringsplit($Line,":")
$String2 = $String2[2]
EndIf
If StringLeft($Line, 13) = "Logical size:" Then
$String3 = stringsplit($Line,":")
$String3 = $String3[2]
EndIf
WEnd
FileClose ($file)
MsgBox(0, '', 'Voici les information recherchées :' & @CRLF & @CRLF & _
$String1 & @CRLF & _
$String2 & @CRLF & _
$String3)
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:32
par zeshrek
Il suffit juste de remplacer les 3 dernieres lignes de son code par :
Code : Tout sélectionner
StringTrimLeft($String1,5) & @CRLF & _
StringTrimLeft($String2,11) & @CRLF & _
StringTrimLeft($String3,13))
pour 'couper' la partie gauche des chaines jusqu'aux guillemets (vu qu'on a le nombre de caracteres recherchés qq lignes plus haut pour identifier els chaines)
Edit:
Quoi que ca serait plus logique de modifier plutot les lignes précédentes, ou se fait la recherche, et de laisser les 3 dernieres telles quelles :
Code : Tout sélectionner
If StringLeft($Line, 5) = "UUID:" Then $String1 = StringTrimLeft($Line,5)
If StringLeft($Line, 11) = "Accessible:" Then $String2 = StringTrimLeft($Line,11)
If StringLeft($Line, 13) = "Logical size:" Then $String3 = StringTrimLeft($Line13)
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:34
par ani
hello,
normal qu'il renvoie la ligne complete
Rajoute un StringReplace(chaine,mot a remplacer,par ce mot)
je met pas le code complet juste les lignes à changer.
Code : Tout sélectionner
If StringLeft($Line, 5) = "UUID:" Then $String1 = StringReplace($Line,"UUID:","")
If StringLeft($Line, 11) = "Accessible:" Then $String2 = StringReplace($Line,"Accessible:","")
If StringLeft($Line, 13) = "Logical size:" Then $String3 = StringReplace($Line,"Logical size:","")
bonne continuation.
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:35
par Laddy
Ok je commence à comprendre.
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
If StringLeft($Line, 5) = "UUID:" Then $String1 = $Line
If StringLeft($Line, 11) = "Accessible:" Then $String2 = $Line
If StringLeft($Line, 13) = "Logical size:" Then $String3 = $Line
WEnd
FileClose ($file)
MsgBox(0, '', 'Voici les information recherchées :' & @CRLF & @CRLF & _
StringTrimLeft($String1,5) & @CRLF & _
StringTrimLeft($String2,11) & @CRLF & _
StringTrimLeft($String3,13))
Zut Ani je viens juste de voir ta réponse, je regarde de suite
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:36
par Laddy
C'est parfait merci à tous 3 pour votre aide. Ce n'est pas évident pour moi.
Re: [..] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:37
par Tlem
Ce que dit zeshrek n'est pas faut, mais dans ce cas je préfèrerais ceci :
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
If StringLeft($Line, 5) = "UUID:" Then $String1 = StringTrimLeft($Line,5)
If StringLeft($Line, 11) = "Accessible:" Then $String2 = StringTrimLeft($Line,11)
If StringLeft($Line, 13) = "Logical size:" Then $String3 = StringTrimLeft($Line,13)
WEnd
FileClose ($file)
MsgBox(0, '', 'Voici les information recherchées :' & @CRLF & @CRLF & _
$String1 & @CRLF & _
$String2 & @CRLF & _
$String3)
Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:38
par zeshrek
Erf Tlem, ton msg a croisé mon édit
Les grands esprits... tout ca...

Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:45
par ani
heu pkoi ?
Moi je changerai directement le StringLeft par StringTrimLeft ?
Stringleft lit les x caractère de la chaine en partant de la gauche
StringTrimLeft lit la chaine en partant du Xième caractère en partant de la gauche
ex: stringleft("ani a toujours tord",3) => renvoie ani
ex: stringTrimLeft("ani a toujours tord",3) => renvoie a toujours tord
Code : Tout sélectionner
If StringTrimLeft($Line, 5) = "UUID:" Then $String1 = $Line
If StringTrimLeft($Line, 11) = "Accessible:" Then $String2 = $Line
If StringTrimLeft($Line, 13) = "Logical size:" Then $String3 = $Line
la fatigue syrement :p
bonne continuation

Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 15:59
par Laddy
Merci encore
voila ce que ça donne :
Me manquerait plus qu'une fonction pour calculer les Mo en Go pour faire plus beau lol
Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 16:05
par ani
ok
un petit bout de code maison
Code : Tout sélectionner
Func ReadBytes($iBytes)
;Cette fonction convertie une valeur en bytes
;Ex: 87298213 retournera 83.25 MB
;Information = http://fr.wikipedia.org/wiki/Bit
If $iBytes <= 1023 Then
$Read = $iBytes & " B" ;Bytes
ElseIf $iBytes <= (1024 ^ 2) And $iBytes >= 1024 Then
$Read = Round($iBytes / 1024, 2) & " KB" ;Kilobytes
ElseIf $iBytes <= (1024 ^ 3) And $iBytes >= (1024 ^ 2) Then
$Read = Round($iBytes / (1024 ^ 2), 2) & " MB" ;Megabytes
ElseIf $iBytes <= (1024 ^ 4) And $iBytes >= (1024 ^ 3) Then
$Read = Round($iBytes / (1024 ^ 3), 2) & " GB" ;Gigabytes
ElseIf $iBytes <= (1024 ^ 5) And $iBytes >= (1024 ^ 4) Then
$Read = Round($iBytes / (1024 ^ 4), 2) & " TB" ;Terabytes
ElseIf $iBytes <= (1024 ^ 6) And $iBytes >= (1024 ^ 5) Then
$Read = Round($iBytes / (1024 ^ 5), 2) & " PB" ;Petabytes
ElseIf $iBytes <= (1024 ^ 7) And $iBytes >= (1024 ^ 6) Then
$Read = Round($iBytes / (1024 ^ 6), 2) & " EB" ;Exabytes
ElseIf $iBytes <= (1024 ^ 8) And $iBytes >= (1024 ^ 7) Then
$Read = Round($iBytes / (1024 ^ 7), 2) & " ZB" ;Zettabytes
ElseIf $iBytes <= (1024 ^ 9) And $iBytes >= (1024 ^ 8) Then
$Read = Round($iBytes / (1024 ^ 8), 2) & " YB" ;Yottabytes
EndIf
Return $Read
EndFunc
dans ton code enfin je sait pas lequel t'a choisi ^^'
en partant du code moi lol
voilàvoilà
ps:sinon ton probleme avec fileopendialog est reglé ?
Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 16:12
par bloodwolff
Code : Tout sélectionner
Func Mo_To_Go($mo, $decimal = 3)
Return Round($mo/1024, $decimal)
EndFunc
Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 16:17
par Laddy
Oh merci à tous deux

j'etais partie dans la recherche.
bloodwolff > ça veut dire que tu réduis le code de Ani en 3 lignes ?
Edit
Ani >
j'ai utilisé ceci :
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
If StringLeft($Line, 5) = "UUID:" Then $String1 = StringTrimLeft($Line,5)
If StringLeft($Line, 11) = "Accessible:" Then $String2 = StringTrimLeft($Line,11)
If StringLeft($Line, 13) = "Logical size:" Then $String3 = StringTrimLeft($Line,13)
WEnd
FileClose ($file)
GUICtrlSetData($UUID,$String1)
GUICtrlSetData($Valid,$String2)
GUICtrlSetData($SizeHD,$String3)
Non pour FileOpenDialog > le fichier est traité à l'endroit ou cela s'ouvre.
Même en mettant des guillements à l'un ou à l'autre le fichier ne veut pas se créer ou je veux.
Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 16:20
par bloodwolff
Tente un
, car un fileopendialog() modifie le working dir
Une ligne aurait suffit mais je te l'ai entouré d'une fonction

mais le code d'ani est plus complet quand même

Re: [R] Extraire 3 informations d'un txt
Posté : mer. 15 déc. 2010 16:46
par ani
J'AVAIS PAS TOUT LU, se sont des octet et non des bytes, mouarf
la fonction de bloodwolf et mieux pour ton usage, vu que la conversion ce fait déjà sur le kiloOctet :p moi je part de la plus base des valeurs donc non fonctionnel dans ton cas
ok, pour le code mais teste
http://www.autoitscript.fr/forum/viewto ... 297#p40297
çà retourne la même chose
bon je vais dl le programme VirtualBox.
Re: [R] Extraire 3 informations d'un txt
Posté : jeu. 16 déc. 2010 12:12
par Laddy
Bonjour
Ani j'ai testé ton code à la place de ce que j'avais mis et malheureusement mes champs restent vides.
Code : Tout sélectionner
$file = FileOpen("InfoSourceVDI.txt", 0)
Global $String1, $String2, $String3
While 1
$Line = FileReadLine($file)
If @error Then ExitLoop
;If StringLeft($Line, 5) = "UUID:" Then $String1 = StringTrimLeft($Line,5)
;If StringLeft($Line, 11) = "Accessible:" Then $String2 = StringTrimLeft($Line,11)
;If StringLeft($Line, 13) = "Logical size:" Then $String3 = StringTrimLeft($Line,13)
If StringTrimLeft($Line, 5) = "UUID:" Then $String1 = $Line
If StringTrimLeft($Line, 11) = "Accessible:" Then $String2 = $Line
If StringTrimLeft($Line, 13) = "Logical size:" Then $String3 = $Line
WEnd
FileClose ($file)
GUICtrlSetData($UUID,$String1)
GUICtrlSetData($Valid,$String2)
GUICtrlSetData($SizeHD,Mo_To_Go($String3) &" Go")
bloodwolff > merci pour la fonction
Me reste à trouver comment afficher une couleur différente pour le champ accessibilité

c'est un détail mais bon c'est comme ça que j'apprends le langage
Le plus dur reste à venir le clonage du vdi car là je me demande comment je vais gérer le pourcentage effectué de la commande

Re: [R] Extraire 3 informations d'un txt
Posté : jeu. 16 déc. 2010 14:38
par bloodwolff
Regarde du coté de
Devancer par l'autre post ;@ Je devrais apprendre à lire !
Re: [R] Extraire 3 informations d'un txt
Posté : jeu. 16 déc. 2010 14:57
par Laddy
Bloodwolff > merci mais il s'agit de modifier la couleur du texte de l'input en fonction du résultat trouvé par $string2.
if $string2 = "yes" alors changer la couleur en vert
if $string2 = "no" alors changer la couleur en rouge
Re: [R] Extraire 3 informations d'un txt
Posté : jeu. 16 déc. 2010 15:00
par zeshrek
Cette question est traitée dans l'autre topic... (parceque bon, si on commence a se mélanger les questions et les réponses, on va finir par se faire engueuler par le Boss)