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 :

Appliquer le script sur les fichiers de répertoire


Sujet :

VBScript

  1. #1
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut Appliquer le script sur les fichiers de répertoire
    Slt a tous
    J'ai un script qui modifie un fichier File.txt qui se trouve dans le repertoire Script
    et je veux appliquer ce script sur tous les fichiers qui se trouvent dans le même répertoire
    sa veut dire: les modifier tous par un seule script

    voici le script que je l'utilise
    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
     
    Set objFS = CreateObject("Scripting.FileSystemObject")
    strFile = "C:\Documents and Settings\ABBAN\Bureau\Script\File.txt"
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
     
        If InStr(strLine,"AAAAA")> 0 Then
        	strLine = Replace(strLine,"AAAAA","BBBBB")
        End If 
    If InStr(strLine,"CCCCC")> 0 Then
        	strLine = Replace(strLine,"CCCCC","ZZZZZ")
        End If 
        WScript.Echo strLine 
    Loop

  2. #2
    Modérateur
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

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

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 077
    Points : 17 182
    Points
    17 182
    Par défaut
    Salut, bienvenue sur DVP

    En adaptant/utilisant Comment lister les fichiers d'un dossier ? de la FAQ VBScript, en la reliant ce code avec celui que tu proposes, cela devrait fonctionner.
    Soyez sympa, pensez -y
    Balises[CODE]...[/CODE]
    Balises[CODE=NomDuLangage]...[/CODE] quand vous mettez du code d'un autre langage que celui du forum ou vous postez.
    Balises[C]...[/C] code intégré dans une phrase.
    Balises[C=NomDuLangage]...[/C] code intégré dans une phrase quand vous mettez du code d'un autre langage que celui du forum ou vous postez.
    Le bouton en fin de discussion, quand vous avez obtenu l'aide attendue.
    ......... et pourquoi pas, pour remercier, un pour celui/ceux qui vous ont dépannés.
    👉 → → Ma page perso sur DVP ← ← 👈

  3. #3
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    Pouvez vous me guider pour adapter le code.
    Car je suis débutant.
    Merci pou l'aide ProgElecT

  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
    Par défaut

    Ce code est à tester chez vous et dites-moi, s'il y a un problème ou non
    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
    Dim stRep 'Nom du répertoire à parcourir
    Dim oFSO,oFl,Ext 
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    stRep = Parcourir_Dossier()
    MsgBox stRep,64,"Chemin du dossier à traiter"
    If oFSO.FolderExists(stRep) Then
        For each oFl in  oFSO.GetFolder(stRep).Files
    'ici dans ce tableau vous pouvez ajouter d'autres extensions de type texte
            Ext = Array(".txt",".asp",".php",".rtf",".html",".htm",".hta",".xml",".csv",".vbs",".js",".css",".ini",".inf")
            For i=LBound(Ext) To UBound(Ext)
                If instr(lcase(oFl.name),Ext(i)) > 0 Then 
                    Set ReadFile = oFSO.OpenTextFile(oFl,1)
                    Do Until ReadFile.AtEndOfStream
                        strLine = ReadFile.ReadLine
                        If InStr(strLine,"AAAAA")> 0 Then
                            strLine = Replace(strLine,"AAAAA","BBBBB")
                            WScript.Echo strLine
                        End If 
                        If InStr(strLine,"CCCCC")> 0 Then
                            strLine = Replace(strLine,"CCCCC","ZZZZZ")
                            WScript.Echo strLine
                        End If 
                    Loop
                End if
            Next 
        Next
    End If
    '*************************************************************************
    Function Parcourir_Dossier()
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0, "Veuillez choisir un dossier pour le traitement !",1,"c:\Programs")
        If objFolder Is Nothing Then
            Wscript.Quit
        End If
        NomDossier = objFolder.title
        Parcourir_Dossier = objFolder.self.path
    end Function
    '*************************************************************************

  5. #5
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    Merci hackoofr
    le script marche
    mais il na pas modifier les fichiers qui se trouve dans le répertoire
    il nous manque le output

  6. #6
    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
    Par défaut

    Testez ce :
    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
    Option Explicit
    Const ForReading = 1
    Const ForWriting = 2
    Dim stRep 'Nom du répertoire à parcourir
    Dim oFSO,oFl,Ext,ReadFile,strLine,OutPutFile,i 
    'ici dans ce tableau vous pouvez ajouter d'autres extensions de type texte
    Ext = Array(".txt",".asp",".php",".rtf",".html",".htm",".hta",".xml",".csv",".vbs",".js",".css",".ini",".inf")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    stRep = Parcourir_Dossier()
    MsgBox qq(stRep),64,"Chemin du dossier à traiter"
    If oFSO.FolderExists(stRep) Then
        For each oFl in  oFSO.GetFolder(stRep).Files
           For i=LBound(Ext) To UBound(Ext)
              If Instr(lcase(oFl.name),Ext(i)) > 0 And  oFl.size <> 0 Then 
                  Set ReadFile = oFSO.OpenTextFile(oFl,ForReading)'Ouvrir le fichier en lecture Seule
                  strLine = ReadFile.ReadAll 'Lecture de la totalité du fichier
                  Set OutPutFile = oFSO.OpenTextFile(oFl,ForWriting) 'Ouvrir le fichier en écriture
                  strLine = Replace(strLine,"AAAAA","BBBBB")
                  strLine = Replace(strLine,"CCCCC","ZZZZZ")
                  OutPutFile.WriteLine strLine
                  MsgBox strLine,64,"Le fichier "& qq(oFl.name) 
              End if
            Next 
        Next
    End If
    '***************************************************************************************************
    Function Parcourir_Dossier()
    Dim objShell,objFolder,NomDossier
    	Set objShell = CreateObject("Shell.Application")
    	Set objFolder = objShell.BrowseForFolder(0, "Veuillez choisir un dossier pour le traitement !",1,"c:\Programs")
    	If objFolder Is Nothing Then
    		Wscript.Quit
    	End If
    	NomDossier = objFolder.title
    	Parcourir_Dossier = objFolder.self.path
    end Function
    '***************************************************************************************************
    Function qq(strIn)
    	qq = Chr(34) & strIn & Chr(34)
    End Function
    '***************************************************************************************************

  7. #7
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    En Fin sa marche très bien.
    Vous êtes le meilleur Hackoofr

  8. #8
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    hackoofr
    votre script ne veut pas foncionner avec cette instruction

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        If InStr(strLine,"11")> 0 Then
        	strLine = Replace(strLine,"11","22") & "33" & Chr(10)
        End If 
        If InStr(strLine,"AA")> 0 Then
    	strLine = Replace(strLine,"AA","BB") & "CC" & Chr(10)
        End If

  9. #9
    Membre confirmé Avatar de pitchalov
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 340
    Points : 582
    Points
    582
    Par défaut
    Bonjour,

    Pour ce besoin, il faut lire (et traiter) le fichier input ligne par ligne et non pas dans sa globalité. Par exemple :
    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
    Set objFso = CreateObject("Scripting.FileSystemObject")
    strInputFolder = "C:\..\Script\"
    For Each objInputFile In objFso.GetFolder(strInputFolder).Files
    	strOutput = ""
    	Set objTmpFile = objFso.OpenTextFile(objInputFile.Path, 1)
    	Do Until objTmpFile.AtEndOfStream
    		strLine = objFile.ReadLine
    		If InStr(strLine,"11")> 0 Then
    			strLine = Replace(strLine,"11","22") & "33" & Chr(10)
    		End If 
    		If InStr(strLine,"AA")> 0 Then
    			strLine = Replace(strLine,"AA","BB") & "CC" & Chr(10)
    		End If
    		strOutput = strOutput  & strLine & vbCrLf
    	Loop
            objTmpFile.Close
    	objFso.OpenTextFile(objInputFile.Path, 2).Write strOutput
    Next
    (non testé)

    Bonne continuation

  10. #10
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    Citation Envoyé par pitchalov Voir le message
    Bonjour,

    Pour ce besoin, il faut lire (et traiter) le fichier input ligne par ligne et non pas dans sa globalité. Par exemple :
    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
    Set objFso = CreateObject("Scripting.FileSystemObject")
    strInputFolder = "C:\..\Script\"
    For Each objInputFile In objFso.GetFolder(strInputFolder).Files
    	strOutput = ""
    	Set objTmpFile = objFso.OpenTextFile(objInputFile.Path, 1)
    	Do Until objTmpFile.AtEndOfStream
    		strLine = objFile.ReadLine
    		If InStr(strLine,"11")> 0 Then
    			strLine = Replace(strLine,"11","22") & "33" & Chr(10)
    		End If 
    		If InStr(strLine,"AA")> 0 Then
    			strLine = Replace(strLine,"AA","BB") & "CC" & Chr(10)
    		End If
    		strOutput = strOutput  & strLine & vbCrLf
    	Loop
            objTmpFile.Close
    	objFso.OpenTextFile(objInputFile.Path, 2).Write strOutput
    Next
    (non testé)

    Bonne continuation
    Ne fonctionne pas Merci

  11. #11
    Membre confirmé Avatar de pitchalov
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 340
    Points : 582
    Points
    582
    Par défaut
    Ok
    Effectivement il y a une variable que j'ai oublié de renommer (il suffit de lire et de corriger --> 3 sec) ce qui peut arriver sur des codes non testés.
    Après, au vu de la formalité de votre retour, je me dis que ce n'était pas forcément une bonne idée d'essayer de vous aider, mais je ne m'y méprendrai plus.

    Bonne continuation, tout de même

  12. #12
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2014
    Messages
    178
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Janvier 2014
    Messages : 178
    Points : 44
    Points
    44
    Par défaut
    Merci pour l'aide comeme quand même

Discussions similaires

  1. Réponses: 1
    Dernier message: 11/02/2011, 14h14
  2. Réponses: 1
    Dernier message: 27/11/2008, 11h41
  3. Script qui liste les repertoires donnant des liens sur les fichiers
    Par PsYKrO dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 22
    Dernier message: 27/03/2008, 08h46
  4. appliquer un scripte sur pls fichiers
    Par mariafan dans le forum Langage
    Réponses: 6
    Dernier message: 28/06/2007, 18h03

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