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 :

Recherche de fichiers avec caractère joker dans un dossier [VBScript]


Sujet :

VBScript

  1. #1
    Membre à l'essai
    Homme Profil pro
    Automaticien
    Inscrit en
    Janvier 2013
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2013
    Messages : 9
    Points : 10
    Points
    10
    Par défaut Recherche de fichiers avec caractère joker dans un dossier [VBScript]
    Bonjour,

    Je me présente AIDEL Mehdi, Automaticien à Saint-Etienne dans la Loire, je besoin de réaliser un petit script en VBS qui tourne sur une application Siemens WinCC Advanced RunTime PC (pour les connaisseurs).

    Ce script doit permettre de rechercher des fichiers CSV dont les noms se ressemblent:
    Ex: Pouvoir ouvrir l'un après l'autre tous les fichiers "rec01*.csv" dans le dossier Jour_8, puis ensuite Dossier Jour_9
    Contenu du dossier Jour 8 (Annee_2013\Mois_1\Jour_8):
    rec00_8H_2m_4s.csv
    rec00_8H_6m_4s.csv
    rec01_12H_32m_2s.csv
    rec01_13H_32m_2s.csv
    rec01_14H_32m_2s.csv
    rec01_15H_32m_2s.csv
    Contenu du dossier Jour 9 (Annee_2013\Mois_1\Jour_9):
    rec01_8H_00m_0s.csv
    rec01_9H_32m_2s.csv
    rec01_10H_32m_2s.csv
    ....

    Je voudrai pouvoir utiliser : Set fso = CreateObject("Scripting.FileSystemObject")
    mais je ne vois pas comment continuer

    J'ai un code qui marche l'OS Windows CE. Mais c'est plus simple avec Windows CE car on peut utiliser "Dir" qui accepte les caractères joker (*) et (?).


    Merci de votre aide

    PS: Est-ce possible d'obtenir La FAQ VBS présente sur le forum en format PDF (le lien pour télécharger en PDF à l'air d'être mort)

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

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

    Un début de piste :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
        Dim fso, folder, fld
     
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set folder = fso.GetFolder("c:\Annee_2013\Mois_1\Jour_8")
        For Each fld in folder
            if Left(fld.name, 5) = "rec01" and right(fld.name, 4) = ".csv" then
               MsgBox fld.name
            end if
        Next
        Set folder = Nothing
        Set fso = Nothing

  3. #3
    Membre à l'essai
    Homme Profil pro
    Automaticien
    Inscrit en
    Janvier 2013
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2013
    Messages : 9
    Points : 10
    Points
    10
    Par défaut
    Merci, je vais implémenter ça dans mon code. Je dirai ce que ça donne

  4. #4
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222

  5. #5
    Membre à l'essai
    Homme Profil pro
    Automaticien
    Inscrit en
    Janvier 2013
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Automaticien
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2013
    Messages : 9
    Points : 10
    Points
    10
    Par défaut
    C'est bon, voici mon Code:

    Pour info, la syntaxe suivante : SmartTags("DB_Rapport_Data_Enregistrement.Nom")
    fait appel à la variable automate : DB_Rapport_Data_Enregistrement.Nom

    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
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
     
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim f_csv, fs_csv
    Dim fs_csv1, fs_csv2, fs_csv3, fs_csv4, fs_csv5, fs_csv6, fs_csv7, fs_csv8, fs_csv_all
    Dim f_csv1, f_csv2, f_csv3, f_csv4, f_csv5, f_csv6, f_csv7, f_csv8, f_csv_all
    Dim Day_csv, Month_csv, Year_csv, nom_rec, nb_char_nom_rec, numero_ligne_csv
    Dim path_search_csv, found_csv, found_csv_path, ligne_csv, nom_voie_ligne, array_cellule_csv, date_de_recherche, file_in_folder
    Dim no_rec_voie, capteur_actif, capteur_nom, date_time_array, date_ligne, heure_ligne, date_time_ligne, ligne_a_copier, no_voie_csv
    Dim fso, folder, fld
     
    'Message : Service occupé, veuillez patientez SVP...
    Call ShowSystemAlarm ("Service occupé, veuillez patientez SVP...!")
     
    nom_rec = SmartTags("DB_Rapport_Data_Enregistrement.Nom")
    nb_char_nom_rec = Len(nom_rec)
    date_de_recherche = SmartTags("Debut_Rec_Day") & "/" & SmartTags("Debut_Rec_Month") & "/" & SmartTags("Debut_Rec_Year") & " " & "00:00:00"
    found_csv = "" 'init
    file_in_folder = False 'init
     
    'Création des objets file pour la gestion des fichiers csv pour chaque capteur et ouverture des ficheirs csv
    Set fs_csv_all = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next  
    Set f_csv_all  = fs_csv_all.OpenTextFile((Path_CSVs & "CSV_All.csv"), ForAppending, True)
     
    Set fs_csv1 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv1  = fs_csv1.OpenTextFile((Path_CSVs & "CSV1.csv"), ForAppending, True)
    Set fs_csv2 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv2  = fs_csv2.OpenTextFile((Path_CSVs & "CSV2.csv"), ForAppending, True)
    Set fs_csv3 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv3  = fs_csv3.OpenTextFile((Path_CSVs & "CSV3.csv"), ForAppending, True)
    Set fs_csv4 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv4  = fs_csv4.OpenTextFile((Path_CSVs & "CSV4.csv"), ForAppending, True)
    Set fs_csv5 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv5  = fs_csv5.OpenTextFile((Path_CSVs & "CSV5.csv"), ForAppending, True)
    Set fs_csv6 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv6  = fs_csv6.OpenTextFile((Path_CSVs & "CSV6.csv"), ForAppending, True)
    Set fs_csv7 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv7  = fs_csv7.OpenTextFile((Path_CSVs & "CSV7.csv"), ForAppending, True)
    Set fs_csv8 = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set f_csv8  = fs_csv8.OpenTextFile((Path_CSVs & "CSV8.csv"), ForAppending, True)
     
    'Recherche des fichiers concernés par l'enregistrement
    Do 
    	'Message : Service occupé, veuillez patientez SVP...
    	Call ShowSystemAlarm ("Service occupé, veuillez patientez SVP...!")
     
    	Day_csv=Day(date_de_recherche)
    	Month_csv=Month(date_de_recherche)
    	Year_csv=Year(date_de_recherche)
     
    	'Chemin approximatif fichier CSV à rechercher
    	path_search_csv = Storage_Path & "Annee_" & Year_csv & "\Mois_" & Month_csv & "\Jour_" & Day_csv & "\" 
     
            Set fso = CreateObject("Scripting.FileSystemObject")
            If fso.FolderExists(path_search_csv) Then
    	  	For Each fld In fso.GetFolder(path_search_csv).Files
            	If Left(fld.Name, nb_char_nom_rec) = nom_rec And Right(fld.Name, 4) = ".csv" Then
    				'Message : Service occupé, veuillez patientez SVP...
    				Call ShowSystemAlarm ("Service occupé, veuillez patientez SVP...!")
     
    				found_csv = fld.Name
    		     	        file_in_folder = True
     
    				'Création d'un objet file system pour la recherche des 'fichiers csv comprenant les valeurs d'archivages
    				Set fs_csv = CreateObject("Scripting.FileSystemObject") 
    				On Error Resume Next  
     
    				'Ouverture du fichier csv trouvé
    				found_csv_path=	Storage_Path & "Annee_" & Year_csv & "\Mois_" & Month_csv & "\Jour_" & Day_csv & "\" & found_csv
    				Set f_csv  = fs_csv.OpenTextFile(found_csv_path, ForReading, True)
     
    				If Err.Number=0 Then
    					'Lire chaque ligne jusqu'à la dernière ligne du fichier
    					numero_ligne_csv = 1 'init
    					Do While f_csv.AtEndOfStream = False
    						ligne_csv = f_csv.ReadLine 'lecture de ligne du fichier csv
    						array_cellule_csv = Split(ligne_csv, ";")'séparation dés données comprise sur la ligne : chaque cellule -> array
    						date_time_ligne = Replace(array_cellule_csv(1),Chr(34),"")'suppression des guillemets générés par le split autour de la date et heure
    						nom_voie_ligne = Replace(array_cellule_csv(0),Chr(34),"")'suppression des guillemets générés par le split
    						date_time_array = Split(date_time_ligne, " ")'séparation de la date et de l'heure
    						date_ligne = date_time_array(0)
    						heure_ligne = date_time_array(1)	
     
    						'Capteur concerné par la lecture de la ligne
    						capteur_actif = False
    						no_rec_voie = 0
    						no_voie_csv = Right(nom_voie_ligne,17)
    						If no_voie_csv = "_record[1].Valeur" Then
    							no_rec_voie = 1
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[1]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[1]")
    						End If
    						If no_voie_csv = "_record[2].Valeur" Then
    							no_rec_voie = 2
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[2]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[2]")
    						End If
    						If no_voie_csv = "_record[3].Valeur" Then
    							no_rec_voie = 3
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[3]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[3]")
    						End If
    						If no_voie_csv = "_record[4].Valeur" Then
    							no_rec_voie = 4
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[4]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[4]")
    						End If
    						If no_voie_csv = "_record[5].Valeur" Then
    							no_rec_voie = 5
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[5]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[5]")
    						End If
    						If no_voie_csv = "_record[6].Valeur" Then
    							no_rec_voie = 6
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[6]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[6]")
    						End If
    						If no_voie_csv = "_record[7].Valeur" Then
    							no_rec_voie = 7
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[7]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[7]")
    						End If				
    						If no_voie_csv = "_record[8].Valeur" Then
    							no_rec_voie = 8
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[8]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[8]")
    						End If
    						If no_voie_csv = "_record[9].Valeur" Then
    							no_rec_voie = 9
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[9]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[9]")
    						End If
    						If no_voie_csv = "record[10].Valeur" Then
    							no_rec_voie = 10
    							capteur_actif = SmartTags("DB_Rapport_Data_Enregistrement.Actif_capteur[10]")
    							capteur_nom = SmartTags("DB_Rapport_Data_Enregistrement.Nom_capteur[10]")
    						End If
     
    						'Recopie de la ligne du fichier CSV dans le Fichier CSV du capteur en cours
    						If capteur_actif Then
    							ligne_a_copier = capteur_nom & ";" & array_cellule_csv(2) & ";" & heure_ligne & ";" & date_ligne & ";" & date_time_ligne
    							Select Case no_rec_voie
    								Case 1
    									f_csv1.WriteLine ligne_a_copier
    								Case 2
    									f_csv2.WriteLine ligne_a_copier
    								Case 3
    									f_csv3.WriteLine ligne_a_copier
    								Case 4
    									f_csv4.WriteLine ligne_a_copier
    								Case 5
    									f_csv5.WriteLine ligne_a_copier
    								Case 6
    									f_csv6.WriteLine ligne_a_copier
    								Case 7
    									f_csv7.WriteLine ligne_a_copier
    								Case 8
    									f_csv8.WriteLine ligne_a_copier
    								Case 9
    									f_csv8.WriteLine ligne_a_copier
    								Case 10
    									f_csv8.WriteLine ligne_a_copier
    								End Select
    						End If
     
    						'Recopie de la ligne du fichier CSV général '(pour affichage de la courbe à partir de ce fichier)
    						If no_rec_voie <> 0 Or numero_ligne_csv = 1 Then
    							f_csv_all.WriteLine ligne_csv
    							numero_ligne_csv = numero_ligne_csv + 1
    						End If
     
    					Loop			
     
    				End If
     
    				'Fermeture du fichier
    				f_csv.Close
     
    				'Libération des objets
    				Set f_csv  = Nothing
    				Set fs_csv  = Nothing
     
    		 	End If
    		Next
    		'on passe au jour suivant (dossier Jour_XX suivant)
    		date_de_recherche = DateAdd("d",1,date_de_recherche)
    	Else 
    		file_in_folder = False	 
    	End If
     
        Set folder = Nothing
        Set fso = Nothing
     
    Loop While (file_in_folder)
     
    'Fermeture des fichiers csv pour chaque capteur
    f_csv_all.Close
    f_csv1.Close
    f_csv2.Close
    f_csv3.Close
    f_csv4.Close
    f_csv5.Close
    f_csv6.Close
    f_csv7.Close
    f_csv8.Close
     
    'Libération des objets
    Set f_csv_all  = Nothing
    Set f_csv1     = Nothing
    Set f_csv2     = Nothing
    Set f_csv3     = Nothing
    Set f_csv4     = Nothing
    Set f_csv5     = Nothing
    Set f_csv6     = Nothing
    Set f_csv7     = Nothing
    Set f_csv8     = Nothing
    Set fs_csv_all = Nothing
    Set fs_csv     = Nothing
    Set fs_csv1    = Nothing
    Set fs_csv2    = Nothing
    Set fs_csv3    = Nothing
    Set fs_csv4    = Nothing
    Set fs_csv5    = Nothing
    Set fs_csv6    = Nothing
    Set fs_csv7    = Nothing
    Set fs_csv8    = Nothing
    AIDEL Mehdi
    Automaticien

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

Discussions similaires

  1. Réponses: 10
    Dernier message: 07/07/2011, 13h19
  2. [AC-2003] Pb avec caractères spéciaux dans fichier
    Par Jean-Luc80 dans le forum VBA Access
    Réponses: 3
    Dernier message: 14/02/2011, 11h46
  3. [XL-2003] Recherche de fichier, avec Joker
    Par Qwerty111 dans le forum Macros et VBA Excel
    Réponses: 20
    Dernier message: 24/06/2009, 11h02
  4. Réponses: 1
    Dernier message: 21/04/2008, 16h28
  5. Recherche de fichier avec joker
    Par defluc dans le forum Langage
    Réponses: 1
    Dernier message: 15/10/2007, 12h27

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