IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Lecture d'un fichier .txt


Sujet :

VBScript

  1. #1
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut Lecture d'un fichier .txt
    Bonjour,

    suite à mon précédant post, ... la réponse rapide m'a d'ailleurs permis de finir la première partie d'écriture de mon script ... Je continue de le développer.
    Je butte maintenant sur la lecture d'un fichier au format TXT écrit avec le bloc note de windows !!! je doit être nul!?

    Pour lire ce fichier qui ne contient qu'une date /heure (par exemple 15/10/2012 17:15:00) j'ai essayer d'utiliser les deux codes ci-dessous.

    Pour le premier, qui ne laisse même pas ouvrir la sélection de répertoire (cela sert pour la suite du script) j'obtiens le message d'erreur suivant : ligne 12, caract 12, fin d'instruction attendue.

    Pour le second, qui ouvre la sélection de répertoire, le message d'erreur est : ligne 12, carct 1, Argument ou appel de procédure incorrect.
    Sur ces deux point je ne m'en sort pas ...

    Mon script en fin d'exécution doit ré ouvrit le fichier Date.txt pour y inscrire la date et l'heure de fin d'exécution qui sera utile à son prochain lancement pour éviter le traitement de fichiers déjà passés à la moulinette.

    en code 3 mon script sans histoire de date et heure, il fonctionne bien.

    Merci d'avance pour vos explications et / ou votre aide.


    Code 1
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo obFolder 'réponse ok, medonne le nom du bon répertoire ....
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    'Wscript.echo "le chemin est :" & PathFichier 'me donne le bon chemin - la ou se trouve le Script et le fichier Date.txt...!!! OK
     
    Dim intfic As Integer
    Dim LastDate As String
    Open "PathFichier.Date.txt" for input as intFic
    Line Input #intFic, LastDate
    Wscript.echo "Date lue" & LastDate
    Close intFic

    Code 2
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo obFolder 'réponse ok, medonne le nom du bon répertoire ....
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    'Wscript.echo "le chemin est :" & PathFichier 'me donne le bon chemin - la ou se trouve le Script et le fichier Date.txt...!!! OK
     
    Set f = FSO.OpenTextFile("PathFichier.Date.txt", ForReading)
    LasteDate = f.ReadLine
    Wscript.echo "Date lue" & LastDate

    Code 3 : script actuellement fonctionnel
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo "obFolder = " & obFolder 'réponse ok, me donne le nom du bon répertoire ....
    Set obFolder2 = obFolder 'y-a-t il un moyen plus élegant que d'utiliser cette ligne?!
    'Wscript.Echo "obFolder2 =" & obFolder2 'idem ci dessus
     
    If NOT obFolder IS Nothing Then
     
    	'Start WellCAD
    	Set WCAD = CreateObject("WellCAD.Application")
    	WCAD.ShowWindow()
     
    	'Get access to input folder and process it
    	'file saveAs avec ajout de _xx à la fin du nom
    	Set FSO = CreateObject("Scripting.FileSystemObject")
     
    	'SaveAs les ancien fichier avec _XX à la fin
    	ProcessFolder WCAD, FSO, obFolder.self.Path
     
    	'Suprime les anciens fichiers sans _XX à la fin
    	Set obFolder = obFolder2
    	'Wscript.Echo "obFolder =" & obFolder 'me donne le bon chemin de départ
    	DeleteOldFiles FSO, obFolder.self.Path
     
    	'Renomme les fichiers _XX et les effaces
    	Set obFolder = obFolder2
    	'Wscript.Echo "obFolder =" & obFolder 'me donne le bon chemin de départ
    	Renomme_XX FSO, obFolder.self.Path
     
     
    	MsgBox "Finished"
     
    Else
     
    	MsgBox "Canceled"
     
    End If
     
     
    Sub ProcessFolder(WCAD, FSO, FolderPath) 'supression de PATH
     
    	'Get access to the folder
    	Set obFolder = FSO.GetFolder(FolderPath)
    	'Wscript.Echo obFolder 'me retourne différent path :
    	'retourne l'arboresence, travail dans chaque rep ou il faut
    	'puis retourne le rep suivant
    	'et ainsi de suite
    	'
    	'Loop on all the files and process each of them
    	For Each obFile In obFolder.Files
     
    		'Check if it is a WCL file containing diagraphie in its name
    		If (StrComp(Right(obFile.Name, 3), "wcl", 1) = 0) AND _
    			(InStr(1, obFile.Name, "_xx", 1) = 0) THEN
     
    			Set obBHDoc = WCAD.OpenBorehole(obFile.Path)
     
    			'SaveAs the WCL file
    			obBHDoc.SaveAs Left(obFile.Path, Len(obFile.Path) - 4) & "_xx" & Right(obFile.Path, 4)
     
     
    			Set obBHDoc = Nothing
    			WCAD.CloseBorehole FALSE
     
    		End If
     
    	Next
     
    	'Loop on all the subfolders and process each of them
    	For Each obSubFolder In obFolder.SubFolders
    		ProcessFolder WCAD, FSO, obSubFolder.Path 'supression de PATH
    	Next
     
    End Sub
     
    'Suprime les anciens fichiers sans _XX à la fin
    Sub DeleteOldFiles (FSO, FolderPath)
     
    'Get access to the folder
    	Set obFolder = FSO.GetFolder(FolderPath)
     
    	'Loop on all the files and process each of them
    	For Each obFile In obFolder.Files
     
    		If (StrComp(Right(obFile.Name, 3), "wcl", 1) = 0) AND _
    		   (InStr(1, obFile.Name, "_xx", 1) = 0) Then
    		    'Wscript.echo obFile.name 'donne le bon nom de fichier
    			'Wscript.echo obFolder 'donne le bon chemin mais sans le \ à la fin
     
     
    		FSO.Deletefile(obfile.path)
     
    		End If
     
    	Next
     
    	'Loop on all the subfolders and process each of them
    	For Each obSubFolder In obFolder.SubFolders
    		DeleteOldfiles FSO, obSubFolder.Path
    	Next
     
    End Sub
     
    'Renomme les anciens fichiers sans _XX à la fin et suprime les orriginaux
    Sub Renomme_XX (FSO, FolderPath)
     
    'Get access to the folder
    	Set obFolder = FSO.GetFolder(FolderPath)
     
    	'Loop on all the files and process each of them
    	For Each obFile In obFolder.Files
     
    		If (StrComp(Right(obFile.Name, 3), "wcl", 1) = 0) AND _
    		   (InStr(1, obFile.Name, "_xx", 1)) Then
    		    'Wscript.echo obFile.name 'donne le bon nom de fichier
    			'Wscript.echo obFolder 'donne le bon chemin mais sans le \ à la fin
    			'Wscript.Echo obfile.path 'donne le bon chemin
    		'Wscript.echo (Len(obfile.path) - 7)
    		'FileName = (Len(obfile.path) - 7) & Right(obfile.path, 4)
    		'Wscript.echo "nom fichier = " & FileName
    		'Wscript.echo obfile.Path
    		FSO.CopyFile obFile.Path, Left(obFile.Path, Len(obFile.Path) - 7) & Right(obFile.Path, 4)
    		'Wscript.echo "fichier à effacer : " & obfile.Path
    		FSO.Deletefile(obfile.path)
     
    		End If
     
    	Next
     
    	'Loop on all the subfolders and process each of them
    	For Each obSubFolder In obFolder.SubFolders
    		Renomme_XX FSO, obSubFolder.Path
    	Next
     
    End Sub

  2. #2
    Membre Expert
    Avatar de pc75
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    3 662
    Détails du profil
    Informations personnelles :
    Âge : 69
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 3 662
    Par défaut
    Bonjour,

    Code 1 :
    En vbs, les variables ne sont pas typées.
    Code 2 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    Set f = FSO.OpenTextFile(PathFichier & "Date.txt", ForReading)

  3. #3
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut Merci - Je vais tester
    Bonjour pc75,

    Et merci pour la réponse.

    Je vais regarder cela dès que j'ai un peu de temps.
    Il me semble tout de même que dans le code 1, la variable intFic est typée en ligne 12 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    Dim intFic AS Integer
    Je vais voir ce qu'il en est pour le code 2

    Bonne journée

  4. #4
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut erreur!
    Mauvaise lecture de ma part .... ok, on ne type pas!!!
    Mais cela ne fonctionne toujours pas!
    Je test le 2 et reviens
    Merci

  5. #5
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut Suite!
    J'ai pris 5 minutes!!!

    pour le code 1:

    Si ligne 12 et 13 les deux variables ne sont pas typées (les AS inactivés par '), je reçois alors : ligne 14, car 29, fin d'instruction attendue
    Pouvez vous m'en dire un peu plus sur ce point????

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo obFolder 'réponse ok, medonne le nom du bon répertoire ....
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    'Wscript.echo "le chemin est :" & PathFichier 'me donne le bon chemin - la ou se trouve le Script et le fichier Date.txt...!!! OK
     
    Dim intfic 'As Integer
    Dim LastDate 'As String
    Open "PathFichier.Date.txt" for input as intFic
    Line Input #intFic, LastDate
    Wscript.echo "Date lue" & LastDate
    Close intFic
    pour le code 2:

    Il faut en plus déclarer les constantes ...
    le code suivant fonctionne donc bien, merci, et je vais essayer d'avancer avec cela.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo obFolder 'réponse ok, medonne le nom du bon répertoire ....
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    'Wscript.echo "le chemin est :" & PathFichier 'me donne le bon chemin - la ou se trouve le Script et le fichier Date.txt...!!! OK
     
    'Déclaration des constantes (ici valable pour ForReading)
    Const ForWriting = 2
    Const ForReading = 1
    Const ForAppending = 8
     
    Set f = FSO.OpenTextFile(PathFichier & "Date.txt", ForReading, True)
    LasteDate = f.ReadLine
    Wscript.echo "Date lue" & Len(LasteDate) 'me retourne 19 c'est bien le nb de caractère de ma date + heure
    Merci pour l'aide

  6. #6
    Membre Expert
    Avatar de pc75
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    3 662
    Détails du profil
    Informations personnelles :
    Âge : 69
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 3 662
    Par défaut
    Re,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Dim intfic 'As Integer
    Dim LastDate 'As String
    intFic = FreeFile
    Open "PathFichier.Date.txt" for input as #intFic

  7. #7
    Expert confirmé
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 127
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 127
    Par défaut
    Salut

    FreeFile, Open, cela ne passe pas en VBScript (en tous cas chez moi )
    :whistle:pourquoi pas, pour remercier, un :plusser: pour celui/ceux qui vous ont dépannés.
    saut de ligne
    OOOOOOOOO👉 → → Ma page perso sur DVP ← ← 👈

  8. #8
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut Merci, Merci
    Je vous remercie pour votre temps et votre aide!

    J'avance doucement, encore bcp à apprendre ... je débute depuis 4/5 jours, et ici à part le net difficile de trouver de la doc!

    Le code 1 qui fonctionne et j'avance dessus.

    Bonne journée à vous.

  9. #9
    Expert confirmé
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 127
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 127
    Par défaut
    Citation Envoyé par topor Voir le message
    Je vous remercie pour votre temps et votre aide!

    J'avance doucement, encore bcp à apprendre ... je débute depuis 4/5 jours, et ici à part le net difficile de trouver de la doc!

    Le code 1 qui fonctionne et j'avance dessus.

    Bonne journée à vous.
    Là tu m'étonnes .

    Tu as la FAQ VBScript et tu devrais récupérer l'aide en ligne (le lien est dans cette même FAQ) Où trouver l'aide en ligne pour VBScript ?
    :whistle:pourquoi pas, pour remercier, un :plusser: pour celui/ceux qui vous ont dépannés.
    saut de ligne
    OOOOOOOOO👉 → → Ma page perso sur DVP ← ← 👈

  10. #10
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut WHY?
    Juste WHY?!!!

    cela fonctionne ... et je lis et écrit dans mon fichier. Je cherche maintenant comment ajouter un retour de ligne à la fin de chaque nouvelle.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    'Get Application object of the Windows shell.
    Set objShell = WScript.CreateObject("Shell.Application")
     
    'Ask the user to select a folder
    Set obFolder = objShell.BrowseForFolder (0, "Select the folder to process", 1)
    'Wscript.Echo obFolder 'réponse ok, medonne le nom du bon répertoire ....
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
    PathFichier = FSO.GetParentFolderName(wscript.ScriptFullName) & "\"
    'Wscript.echo "le chemin est :" & PathFichier 'me donne le bon chemin - la ou se trouve le Script et le fichier Last Date Processing.txt...!!! OK
     
    'Déclaration des constantes (ici valable pour ForReading)
    Const ForWriting = 2
    Const ForReading = 1
    Const ForAppending = 8
     
    Set f = FSO.OpenTextFile(PathFichier & "Last Date Processing.txt", ForReading, False) 'on ne crée pas de nouveau fichier, si omis pas de nouveau fichier
    LasteDate = f.ReadLine
    'Wscript.echo "Date lue (len) :" & Len(LasteDate) 'me retourne 19 c'est bien le nb de caractère de ma date + heure
    'Wscript.echo "Date lue :" & LasteDate 'me retourne bien la date lue dans le fichier
     
    'Get systeme date and time
    Maintenant = Now 'date est heure actuels
    Wscript.echo "Date système :" & Maintenant
    'Write syteme date and time in file Last Date Processing.txt
     Set f = fso.OpenTextFile(PathFichier & "Last Date Processing.txt", ForAppending, TRUE)
     f.write (Maintenant)

  11. #11
    Expert confirmé
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 127
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 127
    Par défaut
    cela fonctionne ...
    Moi je parlais du message 1 code 1.
    Pour un retour ligne, soit VBCrlf ou VBNewLine constantes intrinsèques, donc inutile de les déclarer ni les initialiser.
    :whistle:pourquoi pas, pour remercier, un :plusser: pour celui/ceux qui vous ont dépannés.
    saut de ligne
    OOOOOOOOO👉 → → Ma page perso sur DVP ← ← 👈

  12. #12
    Membre averti
    Homme Profil pro
    Géologue
    Inscrit en
    Septembre 2011
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : Niger

    Informations professionnelles :
    Activité : Géologue
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 41
    Par défaut ok!
    effectivement je n’étais pas remonté si haut!
    J'ai trouvé pour VbCrLf .. ça avance ... doucement!

    encore merci ....

    Je reviendrais quand je ne trouverais pas!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. lecture d'un fichier txt ligne par ligne
    Par SoBaKa dans le forum Général Python
    Réponses: 4
    Dernier message: 06/03/2007, 12h02
  2. Lecture depuis un fichiers txt
    Par mask91 dans le forum C++
    Réponses: 15
    Dernier message: 04/03/2007, 22h40
  3. Réponses: 4
    Dernier message: 13/11/2006, 22h30
  4. Lecture et ecriture fichier txt
    Par jeanmy dans le forum Delphi
    Réponses: 12
    Dernier message: 07/08/2006, 17h23
  5. [debutant] lecture d'un fichier txt de 10mo
    Par karamazov994 dans le forum Entrée/Sortie
    Réponses: 11
    Dernier message: 19/04/2005, 15h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo