在批量配置文件替换线之后开始的exe [英] Replace line in config file with batch afterward start exe

查看:296
本文介绍了在批量配置文件替换线之后开始的exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过一个批处理文件,我想在配置文件中更改值:

 title.connectionString:SERVERIP

SERVERIP是要改变的变量。因此,该批处理文件给谁使用它的选项从4 prefixed IP地址来选择的人。他们选择了4其中一个IP后,配置文件保存新的价值和批处理文件应该运行的可执行文件。

有没有人有一个想法,我怎么可以用一个批处理文件做到这一点?


解决方案

 关闭@echo    SETLOCAL ENABLEEXTENSIONS    拨打:replaceKeyValuefile.configtitle.connectionString123456789    ENDLOCAL退出/ B:replaceKeyValue文件键值
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion
    仁%〜F1,%〜nx1.tmp> NUL&放大器;&安培; (
        (FOR / F标记^ = 1 ^ * ^ ^ delims = ^:^ ^ EOL = %% K的('FINDSTR / N^^<%〜f1.tmp')做(
            回声(%%升| FINDSTR / C:\\%〜2 \\> NUL&放大器;及(
                FOR / F标记= 1 delims =:在(%% L)%% v请勿回波(%%〜五:%〜3
            )|| (
                回声(%%升
            )
        ))GT; %〜F1
        德尔%〜f1.tmp> NUL
    )
    ENDLOCAL

这将处理替换文件中的价值。 Monacraft答案显示,选择所需的IP的完美方式。

这将重命名为file.tmp的文件和文件中的每一行(FINDSTR / n用于避免损失的空行),它是agains传递的关键测试。当需要行被发现,它使用冒号(保持缩进)和值与提供的值替换分裂。所有非匹配的行回显为是。该过程的所有输出发送到原始文件,并在结束时,.tmp文件将被删除。

With a batch file, I want to change a value in a configuration file:

"title.connectionString" : "ServerIP",

ServerIP is the variable to be changed. So the batch file has to give people who use it an option to choose from 4 prefixed IPs. After they selected one of the 4 the IP, the config file has to be saved with new value and the batch file should run an executable.

Has anyone got an idea how I can do this with a batch file?

解决方案

@echo off

    setlocal enableextensions

    call :replaceKeyValue "file.config" "title.connectionString" "123456789"

    endlocal

exit /b

:replaceKeyValue file key value
    setlocal enableextensions disabledelayedexpansion
    ren "%~f1" "%~nx1.tmp" >nul && (
        (for /f tokens^=1^,*^ delims^=^:^ eol^= %%k in ('findstr /n "^" ^<"%~f1.tmp"') do (
            echo(%%l|findstr /c:"\"%~2\"" >nul && (
                for /f "tokens=1 delims=:" %%v in ("%%l") do echo(%%~v : "%~3",
            ) || (
                echo(%%l
            )
        )) > "%~f1" 
        del "%~f1.tmp" > nul
    )
    endlocal

This will handle the replacement of the value in the file. Monacraft answer shows a perfect way of selecting the required ip.

This will rename the file to file.tmp and for each line in the file (findstr /n is used to avoid lost of empty lines) it is tested agains the passed key. When required line is found, it is split using the colon (to keep the indentation) and value replaced with supplied value. All the non matching lines are echoed as is. All the output of the process is send to the original file and at end, the .tmp file is deleted.

这篇关于在批量配置文件替换线之后开始的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆