WSH extraction de données

Résolu
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008 - 4 févr. 2008 à 09:22
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008 - 6 févr. 2008 à 08:05
salut à tous

je suis debutant total dans l'ecriture de script WSH

j'ai reussi , avec grand mal , à genereé des fichiers .TXT dans lesquel se touve quelques renseignemt sur des stations sur mon reseau

j'ai besoin maintenant ,via un second script , d'extraire de ces fichiers la premeire et la seconde ligne et de les mettre dans un fichier excel

les des champs sont des alphanumeriques tout simple

mais je ne sais absolument pas comment faire .............

merci de votre aide

nanou

8 réponses

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
4 févr. 2008 à 16:24
Re,

MsgBox ShowFilesList("C:\TEST"),vbInformation,"Visu Files"

Function ShowFilesList(strPath)
Dim objFso, colFile
Dim strListe
Set objFso = CreateObject("Scripting.FileSystemObject")
strListe = "Répertoire " &vbTab& strPath &vbcr
For Each colFile in objFso.GetFolder(strPath).Files
    If LCase(objFso.GetExtensionName(colFile)) = "txt" Then
       strListe = strListe &vbCrLf& colFile.Path &vbCr&_
                  colFile.Name &vbcrlf
    End if
Next
ShowFilesList = strListe

Set objFso = Nothing
End Function

Eventuellement mapper le lecteur => CreateObject("WScript.Network") + MapNetworkDrive.

jean-marc
3
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
6 févr. 2008 à 06:04
 Bonjour,

Exemple à améliorer:

Call FilesList("C:\TEST")

Private Sub FilesList(strPath)
Dim objFso, objDico, objTxt, colFile
Dim cpt
cpt=0
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objDico = CreateObject("Scripting.Dictionary")
For Each colFile in objFso.GetFolder(strPath).Files
    If LCase(objFso.GetExtensionName(colFile)) = "txt" Then
       Set objTxt = objFso.OpenTextFile(colFile,1)
       objDico.Add cpt, objTxt.ReadLine : cpt = cpt+1
       objDico.Add cpt, objTxt.ReadLine : cpt = cpt+1
       objTxt.Close
       Set objTxt = Nothing
    End if
Next

Call WriteExcel(objDico)
Set objFso = Nothing
Set objDico = Nothing
End Sub

Private Sub WriteExcel(objDico)
Dim objExcel
Dim arrLine, NL
arrLine = objDico.Items

Set objExcel = WScript.CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.ScreenUpdating = False
objExcel.DisplayAlerts = False
objExcel.Workbooks.Add

NL=1
For i=0 To UBound(arrLine) step 2
    objExcel.Cells(NL,1).Value = arrLine(i)
    objExcel.Cells(NL,2).Value = arrLine(i+1)
    NL = NL +1
Next
objExcel.Columns("A:B").Select
objExcel.Selection.Columns.AutoFit
objExcel.Visible = True
objExcel.ScreenUpdating = True
objExcel.Range("A" & NL).Select
MsgBox "fin"
objExcel.ActiveWorkBook.Saved = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = True
objExcel.Quit
Set objExcel = Nothing
End Sub

jean-marc
3
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
4 févr. 2008 à 12:34
 Bonjour à tous,

Simple exemple:

Option Explicit
Const MyFile = "C:\SCRIPTS\Fichiers\fichier1.txt"
Const ForReading = 1

Dim objFso, objTxt
Dim MyLine1, MyLine2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTxt = objFso.OpenTextFile(MyFile,ForReading)

MyLine1 = objTxt.ReadLine
MyLine2 = objTxt.ReadLine
objTxt.Close

Set objTxt = Nothing
Set objFso = Nothing

MsgBox MyLine1 &vbCr& MyLine2

Dim objExcel
Set objExcel = WScript.CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add

objExcel.Cells(1,1).Value = MyLine1
objExcel.Cells(2,3).Value = MyLine2

objExcel.ActiveWorkbook.Close
Set objExcel = Nothing

Attention, pour les questions vbs/wsh, merci de poster dans le bon thème.
Vous êtes ici : [infomsg.aspx Thèmes]

/ [infomsgf_VISUAL-BASIC_1.aspx Visual Basic 6] / [infomsgt_LANGAGES-DERIVES_287.aspx Langages dérivés] / [infomsgt_VBSCRIPT_245.aspx VBScript]

jean-marc
0
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008
4 févr. 2008 à 15:35
merci beaucoup JMO
ça fonctionne surbien
j'arrive à retrouver mes enregistrement
le  dernier petit truc , c'est que  j'ai envriron 300 fichier txt sur un repertoire reseaux
comment je peux faire pour les "scanner" tous à la suite sans avoir à preciser leur nom , juste le repertoire ou ils se touvet ????

merci

ps merci egalement d'avoir corrigé l'endroit ou je dois poser mes question , je ferais attention la prochaine fois

Nanou
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008
5 févr. 2008 à 10:12
Bonjour

merci beaucoup jean marc
mais comme je te l'ai dit je suis vraiment debutant et j'ai beaucoup de mal à relier les deux scripts

j'ai pas besoin de mapper de lecteur reseaux , car dans le script je souhaite lui mettre "en dur" excatement ou se trouve mes fichiers txt

si j'ai bien compris , je remplace ("C:\TEST") par mon chemin reseaux

mais alors apres ............. j'arrive pas à le relier avec le permier script ...
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
5 févr. 2008 à 12:34
 Bonjour,

Dans mon exemple pour Excel, je créé un nouveau classeur.
Mais pour toi:
- faut-il créér un nouveau classeur ?
- faut-il ouvrir un classeur existant ? Plusieurs onglets ?
- dans quelles cellules (colonnes/lignes), faut-il écrire les lines 1 et 2 de chaque .txt ?

jean-marc
0
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008
5 févr. 2008 à 13:07
bonjour !
alors , danqs ton exemple , j'ai repris en effet le fait que tu crées un nouveau classeur excel , avec un seul onglet
par contre j'ai supprimé le fait qu'il essaye de se fermer aussitot

j'ai reussi à placer mes enregistrements dans les deux premieres colonnes
0
cs_nanou666 Messages postés 10 Date d'inscription lundi 8 octobre 2007 Statut Membre Dernière intervention 6 février 2008
6 févr. 2008 à 08:05
salut

absolument ENORME
ça fonctionne parfaitement
je m'etais embarqué dans un script beaucoup plus compliqué et qui ne fonctionne pas du tout

j'espere pouvoir beaucoup apprendre de celui si
fellicitation pour la clareté de ton code
merci beacoup !
0
Rejoignez-nous