You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批处理脚本读取INI文件时如何过滤注释避免报错?

批处理脚本过滤INI注释行的解决方法

问题场景

给定的sample.ini文件内容如下:

[this is a comment1]
Parameter1=sun

[this is a comment2]
Parameter2=is

[this is a comment3]
Parameter3=shining

编写的批处理脚本用于读取该文件并将条目设置为环境变量,原脚本代码:

REM @ECHO OFF

for /f "delims=" %%G in (sample.ini) do set %%G

PAUSE

运行时因INI中的注释行抛出错误:

C:	emp>set [this is a comment1]
Environment variable [this is a comment1] not defined

解决方法

修改for /f循环的参数,添加对注释行的过滤逻辑,修改后的脚本如下:

@ECHO OFF

for /f "eol=[ delims=" %%G in (sample.ini) do set %%G

PAUSE

说明

  • eol=[:指定以[开头的行作为行首注释直接跳过,这样所有类似[this is a comment1]的行都不会进入set命令执行环节,避免报错。
  • delims=:保留整行的原始内容,确保ParameterX=value这类赋值行能完整被set命令解析,正确设置环境变量。

内容的提问来源于stack exchange,提问作者JavaSheriff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:12:05