|
Answer» Well first of all this is my first topic in this forum. I would like to know how can i write & read from a ini file (example.ini) here is the structure of the file:
[Setup] Name=ZyLoR Password=Examplepassword
i would like to know how to for example read from the example.ini file the name and echo it. and in a second batch, the batch file prompt for user and i would write it and the batch file would write a ini with it in this structure:
[Input] Name=
Thanks. Without an OS, it's hard to tell what you have available in the way of tools. If this is XP you can write a logic loop inside a for /f construct. If this is any version of Windows, you could use vbscript as described here . Either way it's the brute force method and NEITHER solution will be the high point of your coding career.
Happy Computing. 8-)
PS. You cannot open a file both input/ouput using the FOR (batch) loop or the FileSystemObject (vbscript).oh sorry... i'm using windows xp sp2 well i'm kinda new in the batch PROGRAMMING thing and i would like to make a batch file that could edit ini files... not a vbscript well i found this code: Code: [Select]@ECHO OFF :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Check command line ECHO.%1 | FIND "?" >NUL IF NOT ERRORLEVEL 1 GOTO Syntax IF [%3]==[] GOTO Syntax :: Check if INI file exists IF NOT EXIST "%~f1" GOTO Syntax
:: Keep variables local SETLOCAL
:: Read variables from command line SET INIFile="%~f1" SET INISection=%~2 SET INIKey=%~3 SET INIValue=
:: Reset temporary variables SET SectOK=0 SET SectFound=0 SET KeyFound=0
:: Search the INI file line by line FOR /F "tokens=* delims=" %%A IN ('TYPE %INIFile%') DO CALL :ParseINI "%%A"
:: Display the result ECHO. IF NOT %SectFound%==1 ( ECHO INI section not found ) ELSE ( IF NOT %KeyFound%==1 ( ECHO INI key not found ) ELSE ( IF DEFINED INIValue ( ECHO.%INIFile% ECHO [%INISection%] ECHO %INIKey%=%INIValue% ) ELSE ( ECHO Value not defined ) ) )
ENDLOCAL & SET INIValue=%INIValue% GOTO:EOF
:ParseINI :: Skip rest of file after key has been found; :: speed improvement by Jeroen Verschuuren IF "%SectFound%"=="1" IF "%KeyFound%"=="1" GOTO:EOF :: Store quoted line in variable SET Line="%~1" :: Check if this line is the required section heading ECHO.%Line%| FIND /I "[%INISection%]" >NUL IF NOT ERRORLEVEL 1 ( SET SectOK=1 SET SectFound=1 GOTO:EOF ) :: Check if this line is a different section header IF "%Line:~1,1%"=="[" SET SectOK=0 IF %SectOK%==0 GOTO:EOF :: Parse any "key=value" line FOR /F "tokens=1* delims==" %%a IN ('ECHO.%Line%') DO ( SET Key=%%a^" SET Value=^"%%b ) :: Strip quotes from key and value SET Value=%Value:"=% SET Key=%Key:"=% :: Check if the key matches the required key IF /I "%Key%"=="%INIKey%" ( SET INIValue=%Value% SET KeyFound=1 ) :: In case the = sign is surrounded by spaces... IF /I "%Key%"=="%INIKey% " ( SET INIValue=%Value% SET KeyFound=1 ) :: ...strip leading space from value IF /I "%Key%"=="%INIKey% " IF "%INIValue:~0,1%"==" " SET INIValue=%INIValue:~1% GOTO:EOF
:Syntax ECHO. ECHO ReadINI.bat, Version 1.20 for Windows NT 4 / 2000 / XP / Server 2003 ECHO Read a value from the specified INI file ECHO. ECHO Usage: READINI "ini_file" "section" "key" ECHO. ECHO Where: "ini_file" is the file name of the INI file to be read ECHO "section" is the section name, without the brackets ECHO "key" is the key WHOSE value must be read ECHO. ECHO Example: if MYPROG.INI looks like this: ECHO [Section 1] ECHO Key1=Value 1 ECHO Key2=Value 2 ECHO [Section 2] ECHO Key1=Value 3 ECHO Key2=Value 4 ECHO. ECHO Then the command: READINI "MYPROG.INI" "section 1" "key2" ECHO should return: Value 2 ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com followed their example.. but it won't work.. does anyone know how to fix this? If you're using XP, the code is a lot of much easy:
First question: Code: [Select]@echo off for /f "tokens=2 delims==" %%A in ('find "Name" example.ini') do echo %%A
Second: Code: [Select]@echo off set /p name="What's your name? " echo [Input] > example2.ini echo Name=%name% >> example2.ini yes that does write the ini but.. i want to read from a ini :S is it possible?
|