|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
  『楼 主』:
[分享] 批处理读取INI文件
使用 LLM 解释/回答一下
:::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
::使用方法:
:: inifile iniFilePath
::例子:
:: inifile c:\boot.ini
:: 读取c:\boot.ini的所有
:: inifile c:\boot.ini ""
:: 读取c:\boot.ini 段的内容
:: inifile c:\boot.ini "" timeout
:: 显示c:\boot.ini 段 timeout的值
::
::::::::::::::::::::::::::::::::::::::::::::2006-12-18::::::::::::
因自己的需要编写的一个批处理,大家可共同完善.
- @echo off
- :::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
- ::使用方法: ::
- :: inifile iniFilePath ::
- ::例子: ::
- :: inifile c:\boot.ini ::
- :: 读取c:\boot.ini的所有 ::
- :: inifile c:\boot.ini "" ::
- :: 读取c:\boot.ini 段的内容 ::
- :: inifile c:\boot.ini "" timeout ::
- :: 显示c:\boot.ini 段 timeout的值 ::
- ::07-04-26 新增设置变量的功能,只需将下面的setvar=0改为1即可 ::
- ::09-11-05 重写并优化部份代码,修正不能正确设置变量的BUG ::
- :: 100:文件不存在或未找到 ::
- ::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::
- SETLOCAL
- set setvar=1
- ::当有指定参娄并且setvar值为1时就将的值设为变量
- ::例子inifile c:\boot.ini "" timeout 就可以得到一个%timeout%的变量
- ::初始化变量
- set exit_code=
- set item=
- set item_value=inifile=没有找到指定项目!
- set filepath=
- set section=
- set inifile=
- if not "%~1"=="" (
- set filepath=%1
- ) else goto :file_err
- if not exist %filepath% goto :file_err
- if "%~2"=="" goto :section
- set "section=%~2"
- set "item=%~3"
- call :开始
- endlocal&if defined item set %item_value%
- goto :eof
- :开始
- 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 (
- call :分析数据 "%%i" "%%j"
- ::强制退出,并返回一个退出代码
- if defined exit_code exit /b %exit_code%
- )
- goto :eof
- :分析数据
- set "a=%~1"
- setlocal EnableDelayedExpansion
- call :trim a
- endlocal & set "a=%a%"
- ::如果获取到的第一个字符是"
- if "%a:~0,1%"=="" (set exit_code=0&goto :eof)
- ::分析数据,因为INI文件一般“;”以后是注释符,这里去掉注释。
- for /f "delims=;" %%x in ("%a%=%~2") do (
- if not DEFINED item (echo %%x) else (
- if /i "%a%"=="%item%" (
- set exit_code=0
- if "%setvar%"=="1" (
- set "item_value=%%x"
- ) else (
- echo %%x
- )
- )
- )
- )
- goto :eof
- :section
- 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
- :file_err
- echo.
- echo %1文件未找到或未输入!
- echo.
- exit /b 100
- goto :eof
chenall 发表于: 2009-11-06 19:17
Last edited by chenall on 2009-11-6 at 19:44 ]
附件
1: inifile.rar (2007-4-26 11:54, 1012 bytes, 下载附件所需积分 1 点
,下载次数: 237)
|

QQ:366840202
http://chenall.net |
|
2006-12-19 01:49 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
建议 chenall 兄把附件里的内容以代码的形式张贴在顶楼,以方便大家直接就帖子内容进行讨论,也方便你对代码进行更新。
It is suggested that Brother chenall post the content in the attachment in the form of code at the top of the thread, so as to facilitate everyone to directly discuss the content of the post and also facilitate you to update the code.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-19 01:54 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
测试有问题,提示命令语法不正确.
There is a problem with the test, and it prompts that the command syntax is incorrect.
|
|
2006-12-19 02:01 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
呵呵,目前的处理方法感觉还不是很好,希望有人可以给出一个更好的方法.
Hehe, the current processing method doesn't feel very good yet. Hope someone can come up with a better approach.
|

QQ:366840202
http://chenall.net |
|
2006-12-19 02:02 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by HUNRYBECKY at 2006-12-19 02:01 AM:
测试有问题,提示命令语法不正确.
请贴出你使用的命令.
未加参数运行也会有这个提示.等下再改了
有可能是没有加"的原因.
建议加的参数都用""
例子:
inifile "c:\boot.ini" "" "timeout"
一楼的代码已经更新,附件未更新,可根据一楼的代码修改一下.
if not exist %1 goto :file_err
移动到下一行改为
if not exist %filepath% goto :file_err
Last edited by chenall on 2006-12-19 at 02:08 AM ]
Originally posted by HUNRYBECKY at 2006-12-19 02:01 AM:
There is a problem with the test, and it prompts that the command syntax is incorrect.
Please post the command you used.
Running without parameters will also have this prompt. I'll modify it later.
It may be because there is no "".
It is recommended to use "" for all added parameters.
Example:
inifile "c:\boot.ini" "" "timeout"
The code on the first floor has been updated, but the attachment has not been updated. You can modify it according to the code on the first floor.
if not exist %1 goto :file_err
Change the line to move to
if not exist %filepath% goto :file_err
Last edited by chenall on 2006-12-19 at 02:08 AM ]
|

QQ:366840202
http://chenall.net |
|
2006-12-19 02:03 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
对 boot.ini 的格式和含义不甚了解,chenall 兄能否帖一个极度BT的示范例子出来,要求里面要出现尽可能多的特殊情况:比如特殊字符啦、比如空行啦、比如光标定位在某行的末尾而不是定位在最后一个空行上啦……只有把最BT的情形都考虑到了,做出来的代码才具备最大的兼容性。
I'm not sure about the specific context and whether it can be accurately translated in a very precise technical sense here. But the text you provided is:
对 boot.ini 的格式和含义不甚了解,chenall 兄能否帖一个极度BT的示范例子出来,要求里面要出现尽可能多的特殊情况:比如特殊字符啦、比如空行啦、比如光标定位在某行的末尾而不是定位在最后一个空行上啦……只有把最BT的情形都考虑到了,做出来的代码才具备最大的兼容性。
Translated:
I don't know much about the format and meaning of boot.ini. Brother chenall, can you post an extremely BT sample example? It is required to have as many special cases as possible: such as special characters, such as blank lines, such as the cursor being positioned at the end of a line instead of at the last blank line... Only by considering all the most BT situations can the made code have the greatest compatibility.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-19 02:11 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
对于像这种格式的
[xxxx]
aaabbbddd ;aaaaaaaaa
cccccccccc ;aaaaaaaaaa
;后面的内容是不读取的.因为;后是注释.
可以试试系统中的INF文件.我目前还没有遇到问题
因为我这个是用来读取INF文件中的信息的.
例子中使用BOOT.INI是为了简单.
For formats like this:
aaabbbddd ;aaaaaaaaa
cccccccccc ;aaaaaaaaaa
;The content after ; is not read. Because ; is a comment.
You can try the INF file in the system. I haven't encountered problems so far.
Because this is for reading information in the INF file.
The example uses BOOT.INI for simplicity.
|

QQ:366840202
http://chenall.net |
|
2006-12-19 02:16 |
|
|
oumigaiiii
新手上路

积分 14
发帖 6
注册 2007-3-13
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
双击没反应,一闪过去了,该怎么用啊?晕
Double-clicking has no response, it flashes by. How to use it? Oh no
|
|
2007-3-13 12:10 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by oumigaiiii at 2007-3-12 23:10:
双击没反应,一闪过去了,该怎么用啊?晕
看看楼主前面写的使用方法!
Originally posted by oumigaiiii at 2007-3-12 23:10:
Double-clicking has no response and it flashes by. How to use it? Oh no
Take a look at the usage method written by the original poster earlier!
|
|
2007-3-13 12:26 |
|
|
oumigaiiii
新手上路

积分 14
发帖 6
注册 2007-3-13
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-3-12 11:26 PM:
看看楼主前面写的使用方法!
我看了,那个例子那几行,在哪输???怎么调用??
Originally posted by lxmxn at 2007-3-12 11:26 PM:
Look at the usage method written by the original poster above!
I read it, where do I enter those lines in that example??? How to call it???
|
|
2007-3-13 13:10 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by oumigaiiii at 2007-3-13 00:10:
我看了,那个例子那几行,在哪输???怎么调用??
在命令提示符CMD和批处理文件中调用,要带参数运行,比如:"inifile c:\windows\control.ini",批处理文件中可以:"call inifile c:\windows\control.ini"
Originally posted by oumigaiiii at 2007-3-13 00:10:
I saw, where to input those lines in that example??? How to call it???
Call it in the command prompt CMD and batch files, and run it with parameters. For example: "inifile c:\windows\control.ini", in a batch file, it can be: "call inifile c:\windows\control.ini"
|
|
2007-3-13 13:14 |
|
|
axi
中级用户
   脚本爱好者
积分 238
发帖 93
注册 2007-3-11 来自 GZ
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
研究一下,因为我也需要这样的程序。
Let's study it, because I also need such a program.
|
|
2007-3-14 01:05 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
忘记加分了,补上
Forgot to add points, make it up
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2007-3-14 02:41 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
|
2007-3-14 03:39 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
还差一个参数没有啊,应该还有一个功能能读出项目的数值 如
timeout=3
得到 3
现在
inifile c:\boot.ini "[boot loader]" timeout
:: 显示c:\boot.ini [boot loader]段 timeout的值
只能读出成
timeout=3
There is still one parameter missing. There should be another function to read out the value of the project. For example, timeout=3 gets 3. Now inifile c:\boot.ini "" timeout
:: Display the value of timeout in the section of c:\boot.ini
Can only read out as
timeout=3
此帖被 +1 点积分 点击查看详情 评分人:【 】 | 分数: +1 | 时间:2009-12-10 12:09 |
|
|
|
2007-3-17 07:52 |
|