:inifile
if not "%~1"=="" (
set filepath=%1
) else goto :file_err
if not exist %filepath% goto :file_err
setlocal EnableDelayedExpansion
if not "%~2"=="" (
set section=%~2
if "!section:~0,1!"==""^" set section=!section:~1!
if "!section:~-1!"==""^" set section=!section:~0,-1!
) else goto :section
if not "%~3"=="" (
set item=%~3
if "!item:~0,1!"==""^" set item=!item:~1!
if "!item:~-1!"==""^" set item=!item:~0,-1!
)
endlocal&set "item=%item%"&set "section=%section%"
for /f "usebackq delims=[] skip=2" %%i in (`find /i "%section%" /n %filepath%`) do set 字段开始=%%i
if "%字段开始%"=="" goto :eof
for /f "eol=; usebackq tokens=1* skip=%字段开始% delims==" %%i in (`type %filepath%`) do (
set a=%%i
setlocal EnableDelayedExpansion
if "!a:~0,1!"=="[" (endlocal&goto :eof)
endlocal
for /f "delims=;" %%x in ("%%i=%%j") do (
if not DEFINED item (echo %%x) else (
setlocal EnableDelayedExpansion
call :trim a
if /i "!a!"=="%item%" (
endlocal&set "item=%%j"
)
)
)
)
goto :eof
:section
endlocal
for /f "eol=; usebackq delims== skip=2" %%i in (`find /i "[" %filepath%`) do echo %%i
goto :eof
:trim
if "!%1:~0,1!"==" " (set %1=!%1:~1!&&goto trim)
if "!%1:~0,1!"==" " (set %1=!%1:~1!&&goto trim)
if "!%1:~-1!"==" " (set %1=!%1:~0,-1!&&goto trim)
if "!%1:~-1!"==" " (set %1=!%1:~0,-1!&&goto trim)
goto :eof
:show_item
if not DEFINED item (echo %b%) else (if /i "%a%"=="%item%" echo %%x)
goto :eof
Originally posted by chenall at 2007-4-26 02:24 PM:
set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根 ...
這個我需要。作者: knoppix7 时间: 2008-5-4 17:46 随便写一个...
没LZ的好.就是短点..
@echo off
::%1 =>INI文件路径
::%2 =>段名 不用加[]
::%3 =>变量名
::%3为空则读出整段内容
::Command =>模式(如 SET/ECHO) {set:设置为变量 echo:显示 },不能为空
::YH =>设置是否使用引号YH="则加引号 YH不存在则不加
::作为子BAT调用的时候变量延迟必须在父BAT中被打开
::作为block调用的时候可以自己看着开
if NOT EXIST "%~1" (Echo File Not Found&GOTO :EOF)
if "%~2"=="" GOTO :EOF
for /F "eol=; tokens=1 delims=:" %%i IN ('findstr /I /n /c:"[%~2]" "%~1" ^| Findstr /v ";" ') DO (
SET line=%%i
)
if "%line%"=="" (echo Section Not Found&GOTO :EOF)
FOR /f "eol=; tokens=1* skip=%line% delims=" %%i IN ('type "%~1"') DO (
set "temp_1=%%i"
if "!temp_1:~0,1!"=="[" (GOTO :EOF) ELSE (
IF "%~3"=="" (%command% %YH%%%i%YH%) ELSE (
FOR /F "tokens=1 delims==" %%a IN (%YH%%%i%YH%) DO (
if /i "%%a"=="%~3" (%command% %YH%%%i%YH%)
)
)
)
)
set temp_1=
测试代码:存为BAT
@echo off&setlocal ENABLEDELAYEDEXPANSION
set YH="
set command=set
echo 设置变量
call :inifile "%~0" 设置变量
set words
pause
echo 显示内容
set YH=
set command=echo
call :inifile "%~0" 显示内容
pause
echo 输出文件
set YH=
set "command=>>test.bat echo"
call :inifile "%~0" 输出文件
call test.bat
pause
del test.bat
GOTO :EOF
:inifile
if NOT EXIST "%~1" (Echo File Not Found&GOTO :EOF)
if "%~2"=="" GOTO :EOF
for /F "eol=; tokens=1 delims=:" %%i IN ('findstr /I /n /c:"[%~2]" "%~1" ^| Findstr /v ";" ') DO (
SET line=%%i
)
if "%line%"=="" (echo Section Not Found&GOTO :EOF)
FOR /f "eol=; tokens=1* skip=%line% delims=" %%i IN ('type "%~1"') DO (
set "temp_1=%%i"
if "!temp_1:~0,1!"=="[" (GOTO :EOF) ELSE (
IF "%~3"=="" (%command% %YH%%%i%YH%) ELSE (
FOR /F "tokens=1 delims==" %%a IN (%YH%%%i%YH%) DO (
if /i "%%a"=="%~3" (%command% %YH%%%i%YH%)
)
)
)
)
set temp_1=
GOTO :EOF
::===========================================
[设置变量]
words=welcome to cn-dos
[显示内容]
welcome to cn-dos
[输出文件]
@echo off
echo output as "%~f0"
echo welcome to cn-dos
pause