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

Scripts/Batch Discussion :

Comment récupérer des variable depuis une section .ini


Sujet :

Scripts/Batch

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Homme Profil pro
    Technicien d'assistance Informatique
    Inscrit en
    Novembre 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Technicien d'assistance Informatique

    Informations forums :
    Inscription : Novembre 2014
    Messages : 5
    Par défaut Comment récupérer des variable depuis une section .ini
    Bonjour,
    voila que je me casse la tête depuis plus de 7 heures non stop pour récupérer des réglages et des variables depuis un fichier ini afin de minimiser mon script:

    de cette manière, pas de souci.
    parametres.ini
    Code ini : 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
     
    [setting1]
    TECHNAME=Nicolas Trantigroin
    NETSET=255.255.255.0
    PROXSET=192.175.2.4
    TEST=Petite phrase1
    [setting2]
    TECHNAME2=Jean Dupont
    NETSET2=192.168.1.1
    PROXSET2=NO PROXY
    TEST2=GRANDE PHRASE
    [setting3]
    TECHNAME3=Mickael Jackson
    NETSET3=10.0.0.1
    PROXSET3=8.8.8.8
    TEST3=Pas de phrase

    test.bat

    Code BAT : 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
    @echo off
     
    title Test ini file
    REM for /f "tokens=1,2 delims==" %%I in ('type test.ini') do SET %%I=%%J
     
    echo %TECHNAME%
    echo %NETSET%
    ECHO %PROXSET%
    ECHO %TEST%
     
    pause >nul
    echo setting1
     
     
    echo %TECHNAME2%
    echo %NETSET2%
    ECHO %PROXSET2%
    ECHO %TEST2%
     
    pause >nul
    echo setting2
     
     
    echo %TECHNAME3%
    echo %NETSET3%
    ECHO %PROXSET3%
    ECHO %TEST3%                               
     
    pause >nul
    echo setting3


    mais voila ce qui m'interesse c'est d'appeller les section [setting1] [setting2] ou [setting3] selon mes besoins.

    Code INI : 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
     
    [setting1]
    TECHNAME=Nicolas Trantigroin
    NETSET=255.255.255.0
    PROXSET=192.175.2.4
    TEST=Petite phrase1
    [setting2]
    TECHNAME=Jean Dupont
    NETSET=192.168.1.1
    PROXSET=NO PROXY
    TEST=GRANDE PHRASE
    [setting3]
    TECHNAME=Mickael Jackson
    NETSET=10.0.0.1
    PROXSET=8.8.8.8
    TEST=Pas de phrase

    test.bat

    Code BAT : 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
    @echo off
     
    title Test ini file
    REM for /f "tokens=1,2 delims==" %%I in ('type test.ini') do SET %%I=%%J
     
    call [setting1]
    echo %TECHNAME%
    echo %NETSET%
    ECHO %PROXSET%
    ECHO %TEST%
     
    pause >nul
     
    echo setting1
     
     
    call [setting2]
    echo %TECHNAME%
    echo %NETSET%
    ECHO %PROXSET%
    ECHO %TEST%
     
    pause >nul
    echo setting2
     
    call [setting3]
    echo %TECHNAME%
    echo %NETSET%
    ECHO %PROXSET%
    ECHO %TEST%                               
     
     
    pause >nul
    echo setting3

    ou pour etre plus précis


    config.ini
    Code INI : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    [dotnet]
    setup=%TMP%\setup.exe
    INSTALL=/r /norestart /log %TEMP%\dotnet.htm
    [firefox]
    setup=%TMP%\setup.exe
    install=/s
    [toto]
    setup=%TMP%\setup.exe
    install=/noreboot /verysilent /autreoption



    install.bat


    Code BAT : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    @echo off
    echo debut de l'install
    call [dotnet]
    %setup% %install%
    call [firefox]
    %setup% %install%
    echo etc etc etc...

    En gros de changer les variable à la volée...

    Merci d'avance.

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Technicien d'assistance Informatique
    Inscrit en
    Novembre 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Technicien d'assistance Informatique

    Informations forums :
    Inscription : Novembre 2014
    Messages : 5
    Par défaut
    J'ai trouvé une piste mais je ne comprend pas tout

    Code BAT : 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
     
    @ECHO OFF
     
    SETLOCAL
    SET INIFile="%~f1"
    SET "FLAG="
     
    for /f "usebackq tokens=1,*eol=|delims==" %%a in (%INIFile%) do (
     IF "%%b"=="" (
      REM No "=" so section
      IF /i "%%a"=="[%2]" (SET flag=Y) ELSE (SET "flag=")
     ) ELSE IF defined flag (
      REM data line - only if FLAG is defined
      REM set values defined
      SET "%%a=%%b"
      REM pick particular values
      if /i "%%a"=="Value1" set "Key1=%%b"
      if /i "%%a"=="Value2" set "Key2=%%b"
      if /i "%%a"=="Value3" set "Key3=%%b"
     )
    )
    SET key
    SET val
     
    GOTO :EOF

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Technicien d'assistance Informatique
    Inscrit en
    Novembre 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Technicien d'assistance Informatique

    Informations forums :
    Inscription : Novembre 2014
    Messages : 5
    Par défaut
    j'ai lu ce lien mais comment mettre ce script n fonction plutôt qu'en script externe?


    En général, on utilise un seul et même script par système pour lire les fichiers *.ini. Celui-ci est ensuite appelé par les scripts ayant besoin de lire un fichier *.ini. Un tel script pourrait ressembler au script 90.

    Code BAT : 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
    @echo off
    setlocal
    setlocal enabledelayedexpansion
     
    set "ScriptName=%~n0"
    set "IniFile="
    set "IniSection="
    set "IniEntrie="
    set "CasseSensitive="
    set "CurrentSection="
     
    :ParseArgs
    if "%~1"=="" goto Init
    if /i "%~1"=="/?" goto Help
    if /i "%~1"=="-?" goto Help
    if /i "%~1"=="/h" goto Help
    if /i "%~1"=="-h" goto Help
    if /i "%~1"=="/help" goto Help
    if /i "%~1"=="-help" goto Help
    if /i "%~1"=="/f" (
        set "IniFile=%~2"
        shift /1
        shift /1
        goto ParseArgs
    )
    if /i "%~1"=="/s" (
        set "IniSection=%~2"
        shift /1
        shift /1
        goto ParseArgs
    )
    if /i "%~1"=="/e" (
        set "IniEntrie=%~2"
        shift /1
        shift /1
        goto ParseArgs
    )
    if /i "%~1"=="/i" (
        set "CasseSensitive=/i"
        shift /1
        goto ParseArgs
    )
    :BadSyntax
    for /f "delims=" %%a in ('net helpmsg 87') do echo [ %~1 ] %%a
    if not "%~0"==":BadSyntax" endlocal
    exit /b 87
     
    :Init
    if not defined IniFile (
        call :UnknowError "/f"
        exit /b 87
    )
    if not exist "%IniFile%" (
        call :UnknowError "%IniFile%"
        exit /b 87
    )
    if defined IniSection goto Exec
    :UnknowError
    if "%~1"=="" (
        call :BadSyntax "/s"
    ) else (
        call :BadSyntax "%~1"
    )
    echo.
    call :Help
    endlocal
    exit /b 87
     
    :Exec
    for /f "usebackq eol=; delims=" %%a in (`type "%IniFile%"`) do (
        set "FixLine=%%~a"
        if "!FixLine:~0,1!"=="[" (
            for /f "tokens=1 delims=[]" %%b in ("%%a") do (
                set "CurrentSection=%%~b"
            )
        ) else (
            if %CasseSensitive% "!CurrentSection!"=="%IniSection%" (
                if defined IniEntrie (
                    for /f "tokens=1 delims==" %%b in ("%%a") do (
                        if %CasseSensitive% "%%~b"=="%IniEntrie%" echo %%a
                    )
                ) else (
                    echo %%a
                )
            )
        )
    )
    goto End
     
    :Help
    echo.
    echo %ScriptName% /f ^<Ini_File_Path^> /s ^<Ini_Section^> [/e ^<Ini_Entrie^>] [/i]
    echo %ScriptName% /?
    echo.
    echo     /f  Définit le fichier *.ini à parser.
    echo     /s  Définit la section à renvoyer.
    echo     /e  Définit l'entrée à renvoyer.
    echo     /i  Définit que la recherche est insensible à la casse.
    echo.    /?  Affiche cette aide.
    echo.
     
    :End
    if not "%~0"==":Help" endlocal
    exit /b 0

  4. #4
    Membre émérite Avatar de devilsnake88
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Janvier 2013
    Messages
    453
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2013
    Messages : 453
    Par défaut
    Bonjour,
    En bidouillant un code trouvé sur Internet j'ai sorti ceci qui est encore à travailler:
    Code bat : 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
    @echo off&cls
    setlocal EnableDelayedExpansion
     
    set "current_path=%~dp0"
    set "current_path_modified=%current_path:~0,-1%"
     
    set INIFILE="%current_path_modified%\config.ini"
    call:getvalue %INIFILE% "setup" "" SETUP "["
    call:getvalue %INIFILE% "install" "" INSTALL "["
    echo.&goto:next
     
    echo SETUP: %SETUP%
    echo INSTALL: %INSTALL%
    goto:eof
     
    :getvalue
    rem This function reads a value from an INI file and stored it in a variable
    rem %1 = name of ini file to search in.
    rem %2 = search term to look for
    rem %3 = group name (not currently used)
    rem %4 = variable to place search result
    REM FOR /F "eol=; tokens=1,2* delims=[" %%x in (%1) DO @echo %%x
    FOR /F "eol=; tokens=1 delims=[]" %%X in ('findstr /b /l /i %~5 %1') DO (
    	set "categorie=%%X"
    	echo - !categorie!
    )
    FOR /F "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO (
    	set "Variable=%~4"
    	set "Value=%%~j"
    	echo - Variable = !Variable! - Value = !Value!
    )
     
    goto:eof
    :next
    pause
    exit

    Ce qui donne:
    - dotnet
    - firefox
    - toto
    - Variable = SETUP - Value = %TMP%\setup.exe
    - Variable = SETUP - Value = %TMP%\setup.exe
    - Variable = SETUP - Value = %TMP%\setup.exe
    - dotnet
    - firefox
    - toto
    - Variable = INSTALL - Value = /r /norestart /log %TEMP%\dotnet.htm
    - Variable = INSTALL - Value = /s
    - Variable = INSTALL - Value = /noreboot /verysilent /autreoption
    En retravaillant un peu le code, je pense que tu pourras faire ce que tu désires.

  5. #5
    Membre Expert
    Avatar de sachadee
    Homme Profil pro
    AMI DU BAT
    Inscrit en
    Janvier 2013
    Messages
    1 478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Brésil

    Informations professionnelles :
    Activité : AMI DU BAT
    Secteur : Distribution

    Informations forums :
    Inscription : Janvier 2013
    Messages : 1 478
    Par défaut [BAT] Executer un .INI par section
    Hello !

    Voici ma solution !

    Il faut juste rajouter à la fin de ton ini la balise

    [end]

    test.ini :


    [dotnet]
    setup=%tmp%\test.bat
    INSTALL=/r /norestart /log %TEMP%\dotnet.htm
    [firefox]
    setup=%tmp%\setup.exe
    install=/s
    [toto]
    setup=%tmp%\setup.exe
    install=/noreboot /verysilent /autreoptio
    [end]

    et ensuite voilà le bat :


    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
    ::By SachaDee - 2015
    @echo off
    setlocal enabledelayedexpansion
    
    call:traite dotnet
    echo --------------
    call:traite firefox
    echo --------------
    call:traite toto
    echo --------------
    exit /b
    
    :traite
    set "setup="
    set "install="
    
    echo Traitement de ==^> %1
    set "$sw=0"
    
    for /f "delims=" %%a in (test.ini) do (
      if !$sw!==1 (
          set "$Ligne=%%a"
          if "!$Ligne:~0,1!"=="[" (
                 goto:end
                 ) else (
                 set "%%a"
           )
       )
       if /i "%%a"=="[%1]" set "$sw=1"
    )
    
    
    goto:eof
    
    :end
    
    echo Envoie de ==^>  !setup! !install!
    call !setup! !install!

Discussions similaires

  1. Réponses: 5
    Dernier message: 02/02/2007, 11h29
  2. Réponses: 2
    Dernier message: 28/11/2006, 15h20
  3. Réponses: 4
    Dernier message: 09/08/2006, 15h14
  4. Réponses: 7
    Dernier message: 23/01/2006, 11h53
  5. [ADO.Net][C#] Comment récupérer des infos d'une table SQL ?
    Par lamojuli dans le forum Accès aux données
    Réponses: 2
    Dernier message: 11/01/2006, 16h00

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