Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 使用批处理文件替换app.config中的键值对_Batch File_Batch Processing - Fatal编程技术网

Batch file 使用批处理文件替换app.config中的键值对

Batch file 使用批处理文件替换app.config中的键值对,batch-file,batch-processing,Batch File,Batch Processing,我有一个app.config,看起来像这样: <!-- Device Configuration goes here --> <DinoConfig Alpha="aaa" Beta="bbb" Gamma="ccc" Theta=""> <Dino Zeta="ooo" Delta="hhh" Tau="rrr" Rho="ddd" /> </DinoConfig> 现在,我想使用批处理文件在D

我有一个app.config,看起来像这样:

   <!-- Device Configuration goes here --> 
         <DinoConfig Alpha="aaa" Beta="bbb" Gamma="ccc" Theta="">
      <Dino Zeta="ooo" Delta="hhh" Tau="rrr" Rho="ddd" />
    </DinoConfig>

现在,我想使用批处理文件在DinoConfig父节点中将Beta改为“yyy”,将Gamma改为“zzz”。我还想更改子节点(Dino)中的键值对

我当前使用批处理脚本的方式:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "string=^<DinoConfig Alpha="nnn" Beta="mmm" Gamma="iii" Theta=""^>"
set "dir=%temp%" & set "file=Dino.config" & pushd "!dir!"
for /f "tokens=*" %%G in (%file%) do (set cstring=%%G
    if "!cstring:~6,8!"=="Beta" set "cstring=%%G"
    echo "!cstring!" | findstr /r /b /c:".*jjj.*" >nul 2>&1 && echo %string% || if defined cstring echo !cstring!
) >> %file:.config=_mod.config%
del %file% & ren %file:.config=_mod.config% %file% & popd
exit /b
@echo关闭
setlocal EnableExtensions EnableDelayedExpansion
设置“字符串=^”
设置“dir=%temp%”和设置“file=Dino.config”&pushd“!dir!”
对于(%file%)中的/f“tokens=*”%%G do(设置cstring=%%G
如果“!cstring:~6,8!”==“Beta”设置“cstring=%%G”
echo“!cstring!”| findstr/r/b/c:'%jjj.*>nul 2>&1&&echo%string%||如果已定义cstring echo!cstring!
)>>%文件:.config=\u mod.config%
del%file%&ren%file:.config=\u mod.config%%file%&popd
退出/b

使用这个,我将更改Beta=“bbb”,在下一次迭代中,我将更改=“vvv”(如果我可以同时更改这两个选项,那将非常方便)。

下面的脚本对我来说很好。将目录和文件值替换为您自己的,目标文件保存在目录中,批处理文件可以从其他文件夹运行:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "string=    ^<Dino DinoName="mmm" Key1="nnn" Key2="pqr" Key3="stu" /^>"
set "dir=%temp%" & set "file=app.txt" & pushd "!dir!"
for /f "tokens=*" %%G in (%file%) do (set cstring=%%G
    if "!cstring:~6,8!"=="DinoName" set "cstring=    %%G"
    echo "!cstring!" | findstr /r /b /c:".*jkl.*" >nul 2>&1 && echo %string% || if defined cstring echo !cstring!
) >> %file:.txt=_mod.txt%
del %file% & ren %file:.txt=_mod.txt% %file% & popd
exit /b

为我工作的代码:

@echo off &setlocal
set "search=jkl"
set "replace=mmm"
(for /f "delims=" %%i in ('findstr "^" "%""C:\App.config""%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%""C:\NewApp.Config""%"
del c:\App.config & ren c:\NewApp.config App.config
set "search=mno"
set "replace=nnn"
(for /f "delims=" %%i in ('findstr "^" "%""C:\App.Config""%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%""C:\NewApp.Config""%"
del c:\App.config & ren c:\NewApp.config App.config

你能告诉我怎样才能做到这一点吗。所以这不是一个免费的代码编写服务,所以如果你希望得到帮助,你需要自己尝试,分享你的努力,并准确描述你的困境;请学习;2.要处理XML数据,您应该使用本机支持该格式的东西;批量脚本只能处理文本文件;将XML视为普通文本并不是一件小事,而且数据被破坏也是相当危险的……我对这一点并不陌生,以前也曾提出过问题。我知道如何编写代码,所以我只想要这种方法。那就足够了。根据你的要求,我正在更新上述问题。我必须说我不太喜欢批处理编程,但我知道我的东西。谢谢,如果你愿意帮助我。嘿,谢谢你发布答案:)。我尝试了你的代码,但它创建的新文件“app.config_mod”是一个空文件。将app.config_mod重命名为app.config时,在ren命令之前的&operator处遇到语法错误。ren命令的问题已修复。看起来您无法为file2 ren{path}/FileName newfilename指定完全限定路径出于格式原因,我已在问题块中添加了输出。请让我知道,如果任何事情在我看来正常,但它产生一个空白的app.config文件,你是否可以找到错误。我尝试过这样做,但仍然产生空文件。设置“字符串=”“我觉得你的脚本看起来不错,但我不明白为什么它会生成空的app_mod.config文件。脚本假定,你的批与所有其他批位于一个单独的目录中,从那里启动它。如果你有一个批量收集,这是有意义的。但您使用的文件(如%file%)位于%temp%或您喜欢的任何其他目录中。如果将所有文件保留在批处理目录中,则不需要
pushd
popd
命令,因为它们将目录更改为%dir%并还原。
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('findstr...
@echo off &setlocal
set "search=jkl"
set "replace=mmm"
(for /f "delims=" %%i in ('findstr "^" "%""C:\App.config""%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%""C:\NewApp.Config""%"
del c:\App.config & ren c:\NewApp.config App.config
set "search=mno"
set "replace=nnn"
(for /f "delims=" %%i in ('findstr "^" "%""C:\App.Config""%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%""C:\NewApp.Config""%"
del c:\App.config & ren c:\NewApp.config App.config