Script WSH de modification d'un fichier texte

Discussions et dépannage concernant Windows NT/2000/XP.

Modérateur : Modérateurs

Répondre
piwu
Membre habitué
Membre habitué
Messages : 109
Enregistré le : 02 juin 2005, 23:00:00

Message par piwu » 23 mai 2003, 05:22:00

Bonjour,

j'ai un petit problème avec un script que j'essaye de mettre en place. Mon but serait d'ouvrir un fichier texte, changer une ligne de ce fichier texte et fermer le fichier texte. Le probleme est que je ne sais pas me positionner à  une ligne précise du fichier texte. Je sais le lire, ou bien effacer tout son contenu pour ecrire d'autres données, mais effacé une seule ligne pour la remplacer, j'ai pas réussi. Bref, c'est du VBS, et si vous avez une solution, voici le script :

<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1><b>Citation :</b></font></TD></TR></TABLE><TABLE BORDER=1 CELLPADDING=10 BORDERCOLOR=#FF0000 ALIGN=CENTER WIDTH=85%><TR BGCOLOR=#F3F2F4><TD><FONT SIZE=-1>
Option Explicit

Dim oSh, oSystemDrive, strCheminFile, oFSO, oBoot

'** Création de l'objet Shell **
Set oSh = WScript.CreateObject("WScript.Shell")

'** Mise en variable des variables d'environnement **
oSystemDrive = oSh.ExpandEnvironmentStrings("%SYSTEMDRIVE%")

'** Mise en variable du chemin du fichier boot.ini **
strCheminFile = (oSystemDrive & "boot.ini")


'** Suppression de tous les attributs du fichiers boot.ini **
oSh.Run ("attrib -R -A -S -H " & strCheminFile)

'** Attente de 2 secondes afin d'arrêter le script pendant que la commande attrib fonctionne **
WScript.Sleep 2000

'** Réinitialisation de tous les attributs du fichiers boot.ini sauf la lecture seule **
'oSh.Run ("attrib +A +S +H " & strCheminFile)

'** Attente de 2 secondes afin d'arrêter le script pendant que la commande attrib fonctionne **
'WScript.Sleep 2000


'** Création de l'objet FileSystemObject **
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

'** Ouverture du fichier boot.ini **
Set oBoot = oFSO.OpenTextFile(strCheminFile, 2)

'?????????????????????????????????????????????????????????????????????????????
'??? Que dois-je mettre ici pour remplacer la seconde ligne par "timeout = 3"?
'?????????????????????????????????????????????????????????????????????????????

'** Fermeture du fichier boot.ini **
oBoot.Close

'** Destructeurs **
Set oSh = nothing
Set oSystemDrive = nothing
Set oFSO = nothing
Set oBoot = nothing
</FONT></TD></TR></TABLE>

merci d'avance

piwu

Kitty
Membre hyperactif
Membre hyperactif
Messages : 771
Enregistré le : 02 juin 2005, 23:00:00
Localisation : CorbeauxLand

Message par Kitty » 23 mai 2003, 12:33:00

Bonjour piwu,

Si tu sais lire/écrire toutes les lignes, pourquoi ne pas passer par un fichier intermédiaire (genre .tmp) pour recopier le contenu du fichier boot.ini. Lors de la recopie, à  la lecture de la ligne "timeout=...", tu écris autre chose. Puis, en final, tu supprimes le fichier boot.ini et tu renommes ton .tmp en boot.ini.
Malheureusement, comme je ne connais pas le VBS, mon explication s'errêtera là .

NB: Vérifie bien que ta routine fonctionne car, sans le fichier boot.ini, le démarrage du pc marchera moins bien.

(Edité à  13:42)

Voici un exemple Visual Basic qui fonctionne. Peut-être trouveras-tu dans le code une correspondance avec ton langage ?

<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1><b>Citation :</b></font></TD></TR></TABLE><TABLE BORDER=1 CELLPADDING=10 BORDERCOLOR=#FF0000 ALIGN=CENTER WIDTH=85%><TR BGCOLOR=#F3F2F4><TD><FONT SIZE=-1>
DIM mLigne AS String
OPEN "C:BOOT.INI" FOR INPUT AS #1
OPEN "C:BOOT.TMP" FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
LINE INPUT #1, mLigne
IF LEFT(mLigne, 8) = "timeout=" THEN
PRINT #2, "timeout=3"
ELSE
PRINT #2, mLigne
END IF
LOOP
CLOSE #2
CLOSE #1
KILL "C:BOOT.INI"
FILECOPY "C:BOOT.TMP", "C:BOOT.INI"
KILL "C:BOOT.TMP"
</FONT></TD></TR></TABLE>

Bye,
Kitty
Image

piwu
Membre habitué
Membre habitué
Messages : 109
Enregistré le : 02 juin 2005, 23:00:00

Message par piwu » 23 mai 2003, 13:02:00

vais essayer ceci, merci (o:

piwu

piwu
Membre habitué
Membre habitué
Messages : 109
Enregistré le : 02 juin 2005, 23:00:00

Message par piwu » 23 mai 2003, 14:23:00

Finalement j'ai réussi ((o:

Merci beaucoup !

voici mon script

<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1><b>Citation :</b></font></TD></TR></TABLE><TABLE BORDER=1 CELLPADDING=10 BORDERCOLOR=#FF0000 ALIGN=CENTER WIDTH=85%><TR BGCOLOR=#F3F2F4><TD><FONT SIZE=-1>Option Explicit

'** Déclaration des variables **
Dim oSh, oSystemDrive, oFSO, oBoot
Dim strCheminFile, strTxt, strTableau, strTimeOut
Dim nTimeOut

'** Initialisation de la variable nTimeOut à  3 secondes **
nTimeOut = 3



'** Création de l'objet Shell **
Set oSh = WScript.CreateObject("WScript.Shell")

'** Mise en variable des variables d'environnement **
oSystemDrive = oSh.ExpandEnvironmentStrings("%SYSTEMDRIVE%")

'** Mise en variable du chemin du fichier boot.ini **
strCheminFile = (oSystemDrive & "boot.ini")


'** Suppression de tous les attributs du fichiers boot.ini **
oSh.Run ("attrib -R -A -S -H " & strCheminFile)

'** Attente de 2 secondes afin d'arrêter le script pendant que la commande attrib fonctionne **
WScript.Sleep 2000


'** Création de l'objet FileSystemObject **
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

'** Ouverture du fichier boot.ini en lecture**
Set oBoot = oFSO.OpenTextFile(strCheminFile, 1, False)

'** Mise en variable du texte de tout le fichier boot.ini **
strTxt = oBoot.ReadAll()

'** Fermeture du fichier boot.ini **
oBoot.Close

'** Mise en tableau du texte du fichier boot.ini avec comme séparation le retour à  la ligne **
strTableau = Split(strTxt, vbCrLf)

'** Mise en variable de la ligne à  remplacer **
strTimeOut = "timeout = " & nTimeOut

'** Remplacement de la seconde ligne du tableau par la nouvelle ligne **
strTableau(1) = strTimeOut

'** Reconstitution du texte du fichier boot.ini **
strTxt = Join(strTableau, vbCrLf)

'** Destructeur de l'objet oBoot **
Set oBoot = nothing

'** Ouverture du fichier boot.ini en écriture**
Set oBoot = oFSO.OpenTextFile(strCheminFile, 2, False)

'** Recopie Intégrale du texte modifié dans le fichier boot.ini **
oBoot.Write strTxt


'** Réinitialisation de tous les attributs du fichiers boot.ini sauf la lecture seule **
'oSh.Run ("attrib +A +S +H " & strCheminFile)

'** Attente de 2 secondes afin d'arrêter le script pendant que la commande attrib fonctionne **
'WScript.Sleep 2000


'** Destructeurs **
Set oSh = nothing
Set oSystemDrive = nothing
Set oFSO = nothing
Set oBoot = nothing</FONT></TD></TR></TABLE>

Répondre

Retourner vers « Windows NT/2000/XP »

Qui est en ligne

Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 32 invités