BAT/Reference

[BAT] ini 파일 읽기

MoongStory 2024. 12. 9. 17:53
반응형

ini 파일의 구조는 아래와 같다고 가정

INI_FileName.ini

[SectionValue]

Entry1="엔트리1"

Entry2="두번째 엔트리"

출처 - http://lallouslab.net/2018/07/23/batchography-parsing-ini-files-from-a-batch-file/

 

Batchography: Parsing INI files from a Batch file

Often times you might want to write Batch file scripts to automate system administration tasks, and in addition to that you might want to pass configuration files to your Batch scripts. This articl…

lallouslab.net

:: 출처 - http://lallouslab.net/2018/07/23/batchography-parsing-ini-files-from-a-batch-file/
@echo off
	:: 
	:: The Batchography book by Elias Bachaalany
	::
:main
	setlocal enabledelayedexpansion
	call :get-ini INI_FileName.ini SectionValue Entry1 result
	echo r=%result%
	call :get-ini INI_FileName.ini SectionValue Ectry2 result
	echo r=%result%
	pause
	goto :eof
:get-ini <filename> <section> <key> <result>
set %~4="Can't get value from ini file."
set insection=
setlocal
	for /f "usebackq eol=; tokens=*" %%a in ("%~1") do (
		set line=%%a
		if defined insection (
			for /f "tokens=1,* delims==" %%b in ("!line!") do (
				if /i "%%b"=="%3" (
					endlocal
					set %~4=%%c
					goto :eof
				)
			)
		)
		if "!line:~0,1!"=="[" (
			for /f "delims=[]" %%b in ("!line!") do (
				if /i "%%b"=="%2" (
					set insection=1
				)else (
					endlocal
					if defined insection goto :eof
				)
			)
		)
	)
	goto :eof
endlocal
반응형

'BAT > Reference' 카테고리의 다른 글

[BAT] 특정 문자를 기준으로 파싱하기  (0) 2024.12.08