bat 读取 ini 文件

bat 读取 ini 文件

参考链接:https://stackoverflow.com/questions/2866117/windows-batch-script-to-read-an-ini-file

这个 bat 支持 ini 的键值与=号之间存在空格,例如 key1 = value1

readini.bat:

@if (@[email protected]) @end /* -- batch / JScript hybrid line to begin JScript comment

:: --------------------
:: ini.bat
:: ini.bat /? for usage
:: --------------------

@echo off
setlocal enabledelayedexpansion

goto begin

:: color code by jeb -- https://stackoverflow.com/a/5344911/1683264
:c
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b
:: but it doesn‘t handle slashes.  :(
:s
<NUL set /p "=/"&exit /b

:usage
for /F "tokens=1,2 delims=#" %%a in (‘"prompt #$H#$E# & echo on & for %%b in (1) do rem"‘) do set "DEL=%%a"
<nul > X set /p ".=."

echo Usage:
call :c 07 "   query:"
call :c 0F " %~nx0 "&call :s&call :c 0F "i item ["&call :s&call :c 0F "s section] inifile"&echo;
call :c 07 "   create or modify:"
call :c 0F " %~nx0 "&call :s&call :c 0F "i item "&call :s&call :c 0F "v value ["&call :s&call :c 0F "s section] inifile"&echo;
call :c 07 "   delete:"
call :c 0F " %~nx0 "&call :s&call :c 0F "d item ["&call :s&call :c 0F "s section] inifile"&echo;
echo;
echo Take the following ini file for example:
echo;
echo    [Config]
echo    password=1234
echo    usertries=0
echo    allowterminate=0
echo;
echo To read the "password" value:
call :c 0F "   %~nx0 "&call :s&call :c 0F "s Config "&call :s&call :c 0F "i password inifile"&echo;
echo;
echo To modify the "usertries" value to 5:
call :c 0F "   %~nx0 "&call :s&call :c 0F "s Config "&call :s&call :c 0F "i usertries "&call :s&call :c 0F "v 5 inifile"&echo;
echo;
echo To add a "timestamp" key with a value of the current date and time:
call :c 0F "   %~nx0 "&call :s&call :c 0F "s Config "&call :s&call :c 0F "i timestamp "&call :s&call :c 0F "v ""%DEL%%%%%date%%%% %%%%time%%%%""%DEL% inifile"&echo;
echo;
echo To delete the "allowterminate" key:
call :c 0F "   %~nx0 "&call :s&call :c 0F "s Config "&call :s&call :c 0F "d allowterminate inifile"&echo;
echo;
call :c 07 "In the above examples, "&call :s
call :c 0F "s Config "
echo is optional, but will allow the selection of
echo a specific item where the ini file contains similar items in multiple sections.
del X
goto :EOF

:begin
if "%~1"=="" goto usage
for %%I in (item value section found) do set %%I=
for %%I in (%*) do (
    if defined next (
        if !next!==/i set "item=%%~I"
        if !next!==/v (
            set modify=true
            set "value=%%~I"
        )
        if !next!==/d (
            set "item=%%~I"
            set modify=true
            set delete=true
        )
        if !next!==/s set "section=%%~I"
        set next=
    ) else (
        for %%x in (/i /v /s /d) do if "%%~I"=="%%x" set "next=%%~I"
        if not defined next (
            set "arg=%%~I"
            if "!arg:~0,1!"=="/" (
                1>&2 echo Error: Unrecognized option "%%~I"
                1>&2 echo;
                1>&2 call :usage
                exit /b 1
            ) else set "inifile=%%~I"
        )
    )
)
for %%I in (item inifile) do if not defined %%I goto usage
if not exist "%inifile%" (
    1>&2 echo Error: %inifile% not found.
    exit /b 1
)

cscript /nologo /e:jscript "%~f0" "%inifile%" "!section!" "!item!" "!value!" "%modify%" "%delete%"

exit /b %ERRORLEVEL%

:: Begin JScript portion */
var inifile = WSH.Arguments(0),
section = WSH.Arguments(1),
item = WSH.Arguments(2),
value = WSH.Arguments(3),
modify = WSH.Arguments(4),
del = WSH.Arguments(5),
fso = new ActiveXObject("Scripting.FileSystemObject"),
stream = fso.OpenTextFile(inifile, 1),

// (stream.ReadAll() will not preserve blank lines.)
data = [];
while (!stream.atEndOfStream) { data.push(stream.ReadLine()); }
stream.Close();

// trims whitespace from edges
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/,‘‘) }

// trim + toLowerCase
String.prototype.unify = function() { return this.trim().toLowerCase(); };

// unquotes each side of "var"="value"
String.prototype.splitEx = function(x) {
    for (var i=0, ret = this.split(x) || []; i<ret.length; i++) {
        ret[i] = ret[i].replace(/^[‘"](.*)[‘"]$/, function(m,$1){return $1});
    };
    return ret;
}

// splices a new element into an array just after the last non-empty element.  If first arg is a number, start at that position and look backwards.
Array.prototype.cram = function() {
    for (var args=[], i=0; i<arguments.length; i++) { args.push(arguments[i]); }
    var i = (typeof args[0] == "number" && Math.floor(args[0]) == args[0]) ? args.shift() : this.length;
    while (i>0 && !this[--i].length) {};
    for (var j=0; j<args.length; j++) this.splice(++i, 0, args[j]);
}

function saveAndQuit() {
    while (data && !data[data.length - 1].length) data.pop();
    var stream = fso.OpenTextFile(inifile, 2, true);
    stream.Write(data.join(‘\r\n‘) + ‘\r\n‘);
    stream.Close();
    WSH.Quit(0);
}

function fatal(err) {
    WSH.StdErr.WriteLine(err);
    WSH.Quit(1);
}

if (section && !/^\[.+\]$/.test(section)) section = ‘[‘ + section + ‘]‘;

if (modify) {
    if (section) {
        for (var i=0; i<data.length; i++) {
            if (data[i].unify() == section.unify()) {
                for (var j=i + 1; j<data.length; j++) {
                    if (/^\s*\[.+\]\s*$/.test(data[j])) break;
                    var keyval = data[j].splitEx(‘=‘);
                    if (keyval.length < 2) continue;
                    var key = keyval.shift(), val = keyval.join(‘=‘);
                    if (key.unify() == item.unify()) {
                        if (del) data.splice(j, 1);
                        else {
                            data[j] = item + ‘=‘ + value;
                            WSH.Echo(value.trim());
                        }
                        saveAndQuit();
                    }
                }
                if (del) fatal(item + ‘ not found in ‘ + section + ‘ in ‘ + inifile);
                data.cram(j ,item + ‘=‘ + value);
                WSH.Echo(value.trim());
                saveAndQuit();
            }
        }
        if (del) fatal(section + ‘ not found in ‘ + inifile);
        data.cram(‘\r\n‘ + section, item + ‘=‘ + value);
        WSH.Echo(value.trim());
        saveAndQuit();
    }
    else { // if (!section)
        for (var i=0; i<data.length; i++) {
            var keyval = data[i].splitEx(‘=‘);
            if (keyval.length < 2) continue;
            var key = keyval.shift(), val = keyval.join(‘=‘);
            if (key.unify() == item.unify()) {
                if (del) data.splice(i, 1);
                else {
                    data[i] = item + ‘=‘ + value;
                    WSH.Echo(value.trim());
                }
                saveAndQuit();
            }
        }
        if (del) fatal(item + ‘ not found in ‘ + inifile);
        data.cram(item + ‘=‘ + value);
        WSH.Echo(value.trim());
        saveAndQuit();
    }
}
else if (section) { // and if (!modify)
    for (var i=0; i<data.length; i++) {
        if (data[i].unify() == section.unify()) {
            for (var j=i + 1; j<data.length; j++) {
                if (/^\s*\[.+\]\s*$/.test(data[j])) fatal(item + ‘ not found in ‘ + section + ‘ in ‘ + inifile);
                var keyval = data[j].splitEx(‘=‘);
                if (keyval.length < 2) continue;
                var key = keyval.shift(), val = keyval.join(‘=‘);
                if (key.unify() == item.unify()) {
                    WSH.Echo(val.trim());
                    WSH.Quit(0);
                }
            }
        }
    }
    fatal(section + ‘ not found in ‘ + inifile);
}
else { // if (item) and nothing else
    for (var i=0; i<data.length; i++) {
        var keyval = data[i].splitEx(‘=‘);
        if (keyval.length < 2) continue;
        var key = keyval.shift(), val = keyval.join(‘=‘);
        if (key.unify() == item.unify()) {
            WSH.Echo(val.trim());
            WSH.Quit(0);
        }
    }
    fatal(item + ‘ not found in ‘ + inifile);
}

readini.bat

用法:

注意:ini.bat 改为自己的 bat 名字

原文地址:https://www.cnblogs.com/ibingshan/p/11102210.html

时间: 2024-10-26 05:58:56

bat 读取 ini 文件的相关文章

C#读取ini文件的方法

最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Specialized; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; namespace test{ /

Linux下读取Ini文件类

Linux下读取Ini文件类 最近项目上有需要读取Ini文件 所谓Ini文件也就是文本文档 并且以 //注释1 /*注释2 [Section] Key1=aaa Key2=bbb 这种形式存在的文档 自己编写了一个类  比较使用 简单 可以跨平台读写INI文件 头文件Ini.h #include <map> #include <string> using namespace std; #define CONFIGLEN 256 enum INI_RES { INI_SUCCESS,

读取INI文件 - Delphi篇

程序经常需要读取一些用户设定值,怎么完成这个过程? B/S程序一般使用XML文件,而C/S程序则使用INI文件. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读取INI的示例. 本篇介绍使用Delphi完成这个过程. 首先,引用单元. uses Windows, SysUtils, Classes, DB, ADODB, StrUtils, Forms, IniFiles; 其中"IniFiles"即是我们要引用的单元. 然后,定义类变量

QT仿照MFC读取INI文件(支持中文)

QT仿照MFC读取INI文件(支持中文) #include <QSettings> #include <QtGui> UINT SEGetPrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName,      INT nDefault, LPCSTR lpFileName) {     UINT nReturn = nDefault;     QString strDefault, strItem, strSection, strKey

利用GetPrivateProfileString读取ini文件的字段

//INIClass读取类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; using UnityEngine; namespace cReadConfigFile { public class INIClass { public string inipath; [

C++ 读取INI文件

Windows操作系统专门为此提供了6个API函数来对配置设置文件进行读.写: GetPrivateProfileInt() 从私有初始化文件获取整型数值GetPrivateProfileString() 从私有初始化文件获取字符串型值GetProfileInt 从win.ini 获取整数值GetProfileString 从win.ini 获取字符串值WritePrivateProfileString 写字符串到私有初始化文件WriteProfileString 写字符串到win.ini 我们

vs读取ini文件

读取string类型: DWORD GetPrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpDefaut,LPSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName); 其中个参数的意思: LPCTSTR lpAppName ------- INI文件中的一个字段名 LPCTSTR lpKeyName -------- lpAppName 下的一个键名,也就是里面具

批处理读取ini文件

ini文件读取 使用方法:      inifile iniFilePath [section] [item] 例子: inifile c:\boot.ini 读取c:\boot.ini的所有[section] inifile c:\boot.ini "[boot loader]" 读取c:\boot.ini [boot loader]段的内容 inifile c:\boot.ini "[boot loader]" timeout 显示c:\boot.ini [bo

读取INI文件插件

GetPrivateProfile.inc 1 /** 2 * AMXX INI 读取模块 3 * 兵联互创|展鸿 编写 4 * 时间 2015-1-12 5 */ 6 7 enum SUIC_DATA 8 { 9 SUIC_STRING = 1, 10 SUIC_INT, 11 SUIC_FLOAT, 12 SUIC_ARRAY_FLOAT, 13 SUIC_ARRAY_INT 14 } 15 16 /** 17 * @说明: 读INI文件 18 * @参数: lpApplicationNam