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 :

Rechercher et modifier un fichier INI


Sujet :

VBScript

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut Rechercher et modifier un fichier INI
    Bonjour,

    Je voudrais faire une recherche d'un fichier INI dans un lecteur réseau ou un repertoire et modifier la valeur d'une clé.
    Voila ce que j'ai Bricolé mais cela ne fonctionne pas. Je voudrais bien une aide SVP.

    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
     
     
    'Rechercher le chemin des fichiers
     
     Function Find (strPath, strFileName)
     Dim MyDir, MyFile, MySubDir
     Dim strResult
     
    path_start = "c:\Temp"
     
     result = Find(path_start, "notes.ini")
     
     If strFileName = Empty Then Exit Function
     Set ObjFSO = CreateObject("Scripting.FileSystemObject")
     
     
     Set MyDir = ObjFSO.GetFolder(strPath)
     
     For Each MyFile In MyDir.Files
     If ObjFSO.GetFileName(MyFile.Name) = strFileName Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
     
     Next
     
     For Each MySubDir In MyDir.SubFolders
     strResult = strResult & Find(strPath & "\" & MySubDir.Name, strFileName)
     Next
    End Function
     
    'Modifier le fichier
     
    WriteINIString "Notes", "MailServer", "CN=BORDS01", strResult
    wscript.echo GetINIString("Notes", "MailServer", "CN=BORDS01", strResult)
     
     
    Sub WriteINIString(Section, KeyName, Value, FileName)
      Dim INIContents, PosSection, PosEndSection
     
      'Get contents of the INI file As a string
      INIContents = GetFile(FileName)
     
      'Find section
      PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
      If PosSection>0 Then
        'Section exists. Find end of section
        PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
        '?Is this last section?
        If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
        'Separate section contents
        Dim OldsContents, NewsContents, Line
        Dim sKeyName, Found
        OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
        OldsContents = split(OldsContents, vbCrLf)
     
        'Temp variable To find a Key
        sKeyName = LCase(KeyName & "=")
     
        'Enumerate section lines
        For Each Line In OldsContents
          If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
            Line = KeyName & "=" & Value
            Found = True
          End If
          NewsContents = NewsContents & Line & vbCrLf
        Next
     
        If isempty(Found) Then
          'key Not found - add it at the end of section
          NewsContents = NewsContents & KeyName & "=" & Value
        Else
          'remove last vbCrLf - the vbCrLf is at PosEndSection
          NewsContents = Left(NewsContents, Len(NewsContents) - 2)
        End If
     
        'Combine pre-section, new section And post-section data.
        INIContents = Left(INIContents, PosSection-1) & _
          NewsContents & Mid(INIContents, PosEndSection)
      else'if PosSection>0 Then
        'Section Not found. Add section data at the end of file contents.
        If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then 
          INIContents = INIContents & vbCrLf 
        End If
        INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
          KeyName & "=" & Value
      end if'if PosSection>0 Then
      WriteFile FileName, INIContents
    End Sub
     
    Function GetINIString(Section, KeyName, Default, FileName)
      Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
     
      'Get contents of the INI file As a string
      INIContents = GetFile(FileName)
     
      'Find section
      PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
      If PosSection>0 Then
        'Section exists. Find end of section
        PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
        '?Is this last section?
        If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
        'Separate section contents
        sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
     
        If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
          Found = True
          'Separate value of a key.
          Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
        End If
      End If
      If isempty(Found) Then Value = Default
      GetINIString = Value
    End Function
     
    'Separates one field between sStart And sEnd
    Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
      Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
      If PosB > 0 Then
        PosB = PosB + Len(sStart)
        Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
        If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
        If PosE = 0 Then PosE = Len(sFrom) + 1
        SeparateField = Mid(sFrom, PosB, PosE - PosB)
      End If
    End Function
     
     
    'File functions
    Function GetFile(ByVal FileName)
      Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
      'Go To windows folder If full path Not specified.
      If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then 
        FileName = FS.GetSpecialFolder(0) & "\" & FileName
      End If
      On Error Resume Next
     
      GetFile = FS.OpenTextFile(FileName).ReadAll
    End Function
     
    Function WriteFile(ByVal FileName, ByVal Contents)
     
      Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
      'On Error Resume Next
     
      'Go To windows folder If full path Not specified.
      If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then 
        FileName = FS.GetSpecialFolder(0) & "\" & FileName
      End If
     
      Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
      OutStream.Write Contents
    End Function
    J'ai trouvé les 2 codes sur le NET, il fonctionne tout les 2 séparément. Maintenant pour les lier et bien je galère vu mon niveau

  2. #2
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    189
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 189
    Points : 212
    Points
    212
    Par défaut
    Je ne trouve pas dans ton code la fonction Find pourtant elle a l'air appelée là :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    result = Find(path_start, "notes.ini")
    Est ce que tu peux aussi préciser ce que tu compte modifier, avec un exemple de fichier ini.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    En faite le script suivant recherche tout les notes.ini indiqué dans "path_start"

    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
     
    Option Explicit
     
     Dim path_start
     Dim Myfso, result
     Set Myfso = CreateObject("Scripting.FileSystemObject")
     
     
     path_start = "c:\Temp"
     
     result = Find(path_start, "notes.ini")
     
     MsgBox result
     
     Function Find (strPath, strFileName)
     Dim MyDir, MyFile, MySubDir
     Dim strResult
     
     If strFileName = Empty Then Exit Function
     strFileName = Ucase(strFileName)
     
     Set MyDir = Myfso.GetFolder(strPath)
     
     For Each MyFile In MyDir.Files
     If Ucase(MyFile.Name) = strFileName Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
     Next
     For Each MySubDir In MyDir.SubFolders
     strResult = strResult & Find(strPath & "\" & MySubDir.Name, strFileName)
     
    Next
     
    Find = strResult
     
    End Function
    Le 2eme code modifie une section dans le fichier notes.ini

    [Notes]
    MailServer=CN=MAIL04

    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
     
    WriteINIString "Notes", "MailServer", "CN=MAIL01/OU=Srv/O=Barclays-France",
    "C:\Temp\notes.ini"
    wscript.echo GetINIString("Notes", "MailServer",
    "CN=MAIL01/OU=Srv/O=Barclays-France", "C:\Temp\notes.ini")
     
    Sub WriteINIString(Section, KeyName, Value, FileName)
     Dim INIContents, PosSection, PosEndSection
     
     'Get contents of the INI file As a string
     INIContents = GetFile(FileName)
     
     'Find section
     PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
     If PosSection>0 Then
       'Section exists. Find end of section
       PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
       '?Is this last section?
       If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
       'Separate section contents
       Dim OldsContents, NewsContents, Line
       Dim sKeyName, Found
       OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
       OldsContents = split(OldsContents, vbCrLf)
     
       'Temp variable To find a Key
       sKeyName = LCase(KeyName & "=")
     
       'Enumerate section lines
       For Each Line In OldsContents
         If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
           Line = KeyName & "=" & Value
           Found = True
         End If
         NewsContents = NewsContents & Line & vbCrLf
       Next
     
       If isempty(Found) Then
         'key Not found - add it at the end of section
         NewsContents = NewsContents & KeyName & "=" & Value
       Else
         'remove last vbCrLf - the vbCrLf is at PosEndSection
         NewsContents = Left(NewsContents, Len(NewsContents) - 2)
       End If
     
       'Combine pre-section, new section And post-section data.
       INIContents = Left(INIContents, PosSection-1) & _
         NewsContents & Mid(INIContents, PosEndSection)
     else'if PosSection>0 Then
       'Section Not found. Add section data at the end of file contents.
       If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
         INIContents = INIContents & vbCrLf
       End If
       INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
         KeyName & "=" & Value
     end if'if PosSection>0 Then
     WriteFile FileName, INIContents
    End Sub
     
    Function GetINIString(Section, KeyName, Default, FileName)
     Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
     
     'Get contents of the INI file As a string
     INIContents = GetFile(FileName)
     
     'Find section
     PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
     If PosSection>0 Then
       'Section exists. Find end of section
       PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
       '?Is this last section?
       If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
       'Separate section contents
       sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
     
       If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
         Found = True
         'Separate value of a key.
         Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
       End If
     End If
     If isempty(Found) Then Value = Default
     GetINIString = Value
    End Function
     
    'Separates one field between sStart And sEnd
    Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
     Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
     If PosB > 0 Then
       PosB = PosB + Len(sStart)
       Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
       If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
       If PosE = 0 Then PosE = Len(sFrom) + 1
       SeparateField = Mid(sFrom, PosB, PosE - PosB)
     End If
    End Function
     
     
    'File functions
    Function GetFile(ByVal FileName)
     Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
     'Go To windows folder If full path Not specified.
     If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
       FileName = FS.GetSpecialFolder(0) & "\" & FileName
     End If
     On Error Resume Next
     
     GetFile = FS.OpenTextFile(FileName).ReadAll
    End Function
     
    Function WriteFile(ByVal FileName, ByVal Contents)
     
     Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
     'On Error Resume Next
     
     'Go To windows folder If full path Not specified.
     If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
       FileName = FS.GetSpecialFolder(0) & "\" & FileName
     End If
     
     Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
     OutStream.Write Contents
    End Function
    Je voudrais lui donner une racine ou il y a 250 fichiers notes.ini et écrire dedans pour indiquer de nouveaux paramètre

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    S'il vous plait, un coup de pouce. Je rame grave.....

    J'essaye d'adapter le script 1 dans le 2..... je m'arrache les cheveux....

    J'aurais peut être une solution dans la semaine d'un collègue. Je déposerais si cela intéresse.

  5. #5
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    189
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 189
    Points : 212
    Points
    212
    Par défaut
    regarde si ca marche cà :

    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
     
    'Petite remise en forme
    Dim path_start
    Dim Myfso, result
    Set Myfso = CreateObject("Scripting.FileSystemObject")
    path_start = "c:\Temp"
    result = Find(path_start, "notes.ini")
    WriteINIString "Notes", "MailServer", "CN=MAIL01/OU=Srv/O=Barclays-France", result
    wscript.echo GetINIString("Notes", "MailServer","CN=MAIL01/OU=Srv/O=Barclays-France", result)
     
     
    Sub WriteINIString(Section, KeyName, Value, FileName)
    	Dim INIContents, PosSection, PosEndSection
    	'Get contents of the INI file As a string
    	INIContents = GetFile(FileName)
    	'Find section
    	PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
    	If PosSection>0 Then
    		'Section exists. Find end of section
    		PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
    		'?Is this last section?
    		If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
    		'Separate section contents
    		Dim OldsContents, NewsContents, Line
    		Dim sKeyName, Found
    		OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
    		OldsContents = split(OldsContents, vbCrLf)
    		'Temp variable To find a Key
    		sKeyName = LCase(KeyName & "=")
    		'Enumerate section lines
    		For Each Line In OldsContents
    			If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
    				Line = KeyName & "=" & Value
    				Found = True
    			End If
    			NewsContents = NewsContents & Line & vbCrLf
    		Next
    		If isempty(Found) Then
    			'key Not found - add it at the end of section
    			NewsContents = NewsContents & KeyName & "=" & Value
    		Else
    			'remove last vbCrLf - the vbCrLf is at PosEndSection
    			NewsContents = Left(NewsContents, Len(NewsContents) - 2)
    		End If
    		'Combine pre-section, new section And post-section data.
    		INIContents = Left(INIContents, PosSection-1) & _
    		NewsContents & Mid(INIContents, PosEndSection)
    	else'if PosSection>0 Then
    		'Section Not found. Add section data at the end of file contents.
    		If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
    			INIContents = INIContents & vbCrLf
    		End If
    		INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
    		KeyName & "=" & Value
    	end if'if PosSection>0 Then
    	WriteFile FileName, INIContents
    End Sub
    Function GetINIString(Section, KeyName, Default, FileName)
    	Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
    	'Get contents of the INI file As a string
    	INIContents = GetFile(FileName)
    	'Find section
    	PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
    	If PosSection>0 Then
    		'Section exists. Find end of section
    		PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
    		'?Is this last section?
    		If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
    		'Separate section contents
    		sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
    		If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
    			Found = True
    			'Separate value of a key.
    			Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
    		End If
    	End If
    	If isempty(Found) Then Value = Default
    	GetINIString = Value
    End Function
    'Separates one field between sStart And sEnd
    Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
    	Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
    	If PosB > 0 Then
    		PosB = PosB + Len(sStart)
    		Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
    		If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
    		If PosE = 0 Then PosE = Len(sFrom) + 1
    		SeparateField = Mid(sFrom, PosB, PosE - PosB)
    	End If
    End Function
    'File functions
    Function GetFile(ByVal FileName)
    	Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
    	'Go To windows folder If full path Not specified.
    	If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
    		FileName = FS.GetSpecialFolder(0) & "\" & FileName
    	End If
    	On Error Resume Next
    	GetFile = FS.OpenTextFile(FileName).ReadAll
    End Function 
    Function WriteFile(ByVal FileName, ByVal Contents)
    	Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
    	'On Error Resume Next
    	'Go To windows folder If full path Not specified.
    	If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
    		FileName = FS.GetSpecialFolder(0) & "\" & FileName
    	End If
    	Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
    	OutStream.Write Contents
    End Function 
    Function Find (strPath, strFileName)
    	Dim MyDir, MyFile, MySubDir
    	Dim strResult
    	If strFileName = Empty Then Exit Function
    	strFileName = Ucase(strFileName)
    	Set MyDir = Myfso.GetFolder(strPath)
    	For Each MyFile In MyDir.Files
    		If Ucase(MyFile.Name) = strFileName Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
    	Next
    	For Each MySubDir In MyDir.SubFolders
    		strResult = strResult & Find(strPath & "\" & MySubDir.Name, strFileName)
    	Next
    	Find = strResult
    End Function

  6. #6
    Nouveau membre du Club
    Inscrit en
    Décembre 2007
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 42
    Points : 35
    Points
    35
    Par défaut
    Tu devrais regarder le code de spantemonium
    Je suis pas un prof mais tu as ecris :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     Function Find (strPath, strFileName)
     Dim MyDir, MyFile, MySubDir
     Dim strResult
     
    path_start = "c:\Temp"
     
     result = Find(path_start, "notes.ini")
    tu as ecris :

    Creer Fonction Find
    Mettre dans path_start le chemin c:\temp
    Mettre dans la variable result le resultat de la fonction Find
    Fin de création de la fonction Find


    Tu appelles ta fonction à l'interieur de cette derniere donc c'était mal parti.

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    Un très grand MERCI spantemonium
    Et un très grand merci à mon collègue de boulot

    Voilà le script FINISH

    Exécuter --> cscript "chemin du script" "chemin des Notes.ini"

    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
    Option Explicit
     
    	On error Resume Next
     
    	Dim path_start, var, i
    	Dim Myfso, Result, tResult
    	Set Myfso = CreateObject("Scripting.FileSystemObject")
     
    	' On récupère le chemin passé en Argument
    	set var = Wscript.Arguments
    	path_start = var(0)
     
    	' Si aucun chemin précisé en argument, on prend un chemin par défaut
    	If path_start = "" Then path_start = "C:\Temp"
     
    	' On récupère la liste des chemins où le fichier recherché est présent
    	result = Find(path_start, "notes.ini")
     
    	' On découpe la liste des chemins pour la traiter chemin par chemin
    	tResult = Split(result,vbcrlf)
     
    	' On traite chaque fichier de la liste un par un
    	For i = 0 to Ubound(tResult) - 1
     
    		'#########################################################################################
    		'	Copier la ligne suivante autant de fois que nécessaire pour chaque modification 
    		'	à apporter aux fichiers notes.ini. 
    		'	Cette modification sera appliquée à tous les fichiers trouvés
    		'#########################################################################################
     
    		WriteINIString "Notes", "SPELL_DIR", "D:\temp", tResult(i)
    		WriteINIString "Notes", "Cache", "D:\temp", tResult(i)
     
    		'#########################################################################################
     
    	Next
     
     
     
    Function Find (strPath, strFileName)
    	Dim MyDir, MyFile, MySubDir
    	Dim strResult
     
    	If strFileName = Empty Then Exit Function
    	strFileName = Ucase(strFileName)
     
    	Set MyDir = Myfso.GetFolder(strPath)
     
    	For Each MyFile In MyDir.Files
    		If Ucase(MyFile.Name) = strFileName Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
    	Next
     
    	For Each MySubDir In MyDir.SubFolders
    		strResult = strResult & Find(strPath & "\" & MySubDir.Name, strFileName)
    	Next
     
    	Find = strResult
     
    End Function
     
    Sub WriteINIString(Section, KeyName, Value, FileName)
    	  Dim INIContents, PosSection, PosEndSection
     
    	  'Get contents of the INI file As a string
    	  INIContents = GetFile(FileName)
     
    	  'Find section
    	  PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
    	  If PosSection>0 Then
    		'Section exists. Find end of section
    		PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
    		'?Is this last section?
    		If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
    		'Separate section contents
    		Dim OldsContents, NewsContents, Line
    		Dim sKeyName, Found
    		OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
    		OldsContents = split(OldsContents, vbCrLf)
     
    		'Temp variable To find a Key
    		sKeyName = LCase(KeyName & "=")
     
    		'Enumerate section lines
    		For Each Line In OldsContents
    		  If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
    			Line = KeyName & "=" & Value
    			Found = True
    		  End If
    		  NewsContents = NewsContents & Line & vbCrLf
    		Next
     
    		If isempty(Found) Then
    		  'key Not found - add it at the end of section
    		  NewsContents = NewsContents & KeyName & "=" & Value
    		Else
    		  'remove last vbCrLf - the vbCrLf is at PosEndSection
    		  NewsContents = Left(NewsContents, Len(NewsContents) - 2)
    		End If
     
    		'Combine pre-section, new section And post-section data.
    		INIContents = Left(INIContents, PosSection-1) & _
    		  NewsContents & Mid(INIContents, PosEndSection)
    	  else'if PosSection>0 Then
    		'Section Not found. Add section data at the end of file contents.
    		If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
    		  INIContents = INIContents & vbCrLf
    		End If
    		INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
    		  KeyName & "=" & Value
    	  end if'if PosSection>0 Then
    	  WriteFile FileName, INIContents
    End Sub
     
    Function GetINIString(Section, KeyName, Default, FileName)
    	Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
     
      'Get contents of the INI file As a string
      INIContents = GetFile(FileName)
     
      'Find section
      PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
      If PosSection>0 Then
    	'Section exists. Find end of section
    	PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
    	'?Is this last section?
    	If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
     
    	'Separate section contents
    	sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
     
    	If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
    	  Found = True
    	  'Separate value of a key.
    	  Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
    	End If
      End If
      If isempty(Found) Then Value = Default
      GetINIString = Value
    End Function
     
    'Separates one field between sStart And sEnd
    Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
      Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
      If PosB > 0 Then
    	PosB = PosB + Len(sStart)
    	Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
    	If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
    	If PosE = 0 Then PosE = Len(sFrom) + 1
    	SeparateField = Mid(sFrom, PosB, PosE - PosB)
      End If
    End Function
     
     
    'File functions
    Function GetFile(ByVal FileName)
      Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
      'Go To windows folder If full path Not specified.
      If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
        FileName = FS.GetSpecialFolder(0) & "\" & FileName
      End If
      On Error Resume Next
     
      GetFile = FS.OpenTextFile(FileName).ReadAll
    End Function
     
    Function WriteFile(ByVal FileName, ByVal Contents)
     
      Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
      'On Error Resume Next
     
      'Go To windows folder If full path Not specified.
      If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
        FileName = FS.GetSpecialFolder(0) & "\" & FileName
      End If
     
      Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
      OutStream.Write Contents
    End Function
    Merci pour ceux qui aide car c'est pas simple quand on a jamais fait de développement

  8. #8
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    Avez vous une idée pour que le script ne respecte pas la CASE ?

    Si l'extension est en majuscule il fait rien

  9. #9
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    189
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 189
    Points : 212
    Points
    212
    Par défaut
    essaie de remplacer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    For Each MyFile In MyDir.Files
    		If Ucase(MyFile.Name) = strFileName Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
    	Next
    par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    For Each MyFile In MyDir.Files
    		If UCase(MyFile.Name) = UCase(strFileName) Then strResult = strResult & strPath & "\" & MyFile.Name & vbCrLf
    	Next

  10. #10
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    spantemonium

    J'ai une deuxième colle je viens de m'apercevoir qu'il est limité dans la recherche, je m explique :

    Je lui donne un répertoire avec 1 fichier INI à modifier cela fonctionne, je lui donne un répertoire avec 100 répertoire en dessous et donc 100 fichier INI et il fait pas tout

    Quelqu'un aurait une idée ?
    Ma méthode de traitement des lignes est elle limitée ?

  11. #11
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    J'ai trouvé mon problème mais pas la solution

    Il sait pas traiter les répertoires à la racine d'un lecteur Réseau

    Si je fais la commande cscript "chemin du script" "C:\Temp"

    Il fait les modifications de tout les notes.ini qu'il trouve. J'ai rajouté

    Il me sort bien le nombre de Notes.ini qu'il trouve

    Si je fais la commande cscript "chemin du script" "P:\Volume-users"

    Il me sort un résultat -1

    HELP SVP

  12. #12
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    L'erreur est : "Erreur d'exécution Microsoft VBScript: Permission refusée"

    ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    For Each MyFile In MyDir.Files
    Quelqu'un a déjà eu le problème ? Je suis Admin du domaine donc tout les droits sur le répertoire ou j'ai même réappliqué les droits

    Help please

  13. #13
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    189
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 189
    Points : 212
    Points
    212
    Par défaut
    permission refusée des fois c'est que le fichier est tenu par ailleurs, qu'il n'est accessible qu'en lecture.

  14. #14
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    Comment je peux le faire continuer si c'est le cas ?
    Mais cela m'étonne c'est pas un fichier tenu le notes.ini

  15. #15
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    189
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 189
    Points : 212
    Points
    212
    Par défaut
    modifie le manuellement pour voir si tu y arrives

  16. #16
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 24
    Points : 8
    Points
    8
    Par défaut
    Bon je craque complet....Marche pas en réseau, enfin la fonction FIND pose problème.

    Qui peut m'aider à adapter le script pour le faire lire dans un fichier txt ou autre les chemins ou se trouve les fichiers "notes.ini"

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

Discussions similaires

  1. [Inno Setup] : modifier un fichier ini
    Par Mimie37 dans le forum Outils
    Réponses: 0
    Dernier message: 30/07/2014, 18h05
  2. modifier un fichier .ini avec vbs
    Par alex61 dans le forum VBScript
    Réponses: 5
    Dernier message: 11/01/2011, 14h23
  3. [innoSetup] Modifier un fichier .ini
    Par YanDerS dans le forum Outils
    Réponses: 16
    Dernier message: 22/11/2010, 09h21
  4. Rechercher et modifier un fichier texte
    Par totonin dans le forum Langage
    Réponses: 1
    Dernier message: 18/05/2009, 14h48
  5. [VBA-E]Modifier un fichier Ini
    Par illight dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 10/10/2006, 10h15

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