使用批处理修改 .properties 文件中的字符串 [英] Modify a string in a .properties file with batch

查看:24
本文介绍了使用批处理修改 .properties 文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行脚本来修改我的 csm.properties 中的某个属性.我查了很多,最后找到了这段代码.

I am trying to modify a certain property in my csm.properties by executing a script. I looked up a lot and in the end, came to this code.

set "search=CLASSPATH"
set "insert=CLASSPATH^=plugins^/Numbering.jar^^:"

set "textFile="%workingPlace%bincsm.properties""

FOR /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    FOR /f "tokens=1*delims==" %%g IN ("%%i") DO (
        IF /i "%%g" == %search% (
            set "line=%%i"
                setlocal enabledelayedexpansion
                >>"%textFile%" echo(!line:%search%=%insert%!
                endlocal
        )ELSE (
        %%i
        )
    )
)

此代码应读取我文件中的每一行并使用 = 作为分隔符.如果代码将CLASSPATH"作为属性,则应修改该行.但似乎找不到 CLASSPATH.

This code should read every line in my file and use = as a delimiter. If the code gets "CLASSPATH" as property, that line should get modified. But it seems like CLASSPATH isn't found.

这就是 csm.properties 的样子:

This is how csm.properties looks like:

#Tue Jul 10 08:50:23 CEST 2018
JAVA_ARGS=-Xmx20000M -DLOCALCONFIG=true -splash:data/splash.png -Dmd.class.path=$java.class.path -Dcom.nomagic.osgi.config.dir=configuration -Desi.system.config=data/application.conf -Dlogback.configurationFile=data/logback.xml -Dsun.locale.formatasdefault=true -Dinitial.user.language=de
JAVA_HOME=jre1.8.0_152
BOOT_CLASSPATH=lib/xalan.jar
MAIN_CLASS=com.nomagic.osgi.launcher.ProductionFrameworkLauncher
MAC_JAVA_ARGS="-Xdock:name=Cameo Systems Modeler" -Xdock:icon=bin/md.icns -Dapple.laf.useScreenMenuBar=true
APP_ARGS=
DEFAULT_MEMORY_SETTINGS_64=-Xmx[30%,1200,4000]M
DEFAULT_MEMORY_SETTINGS_32=-Xmx800M
CLASSPATH=lib/patch.jar:lib/brand_api.jar
CONSOLE=false

修改后,CLASSPATH应该是这样的:

After modifications, CLASSPATHshould look like this:

CLASSPATH=plugins/Numbering.jar:lib/patch.jar:lib/brand_api.jar

推荐答案

更简单...

@echo OFF
setlocal

set "search=CLASSPATH"
set "insert=plugins/Numbering.jar:"

set "textFile=%workingPlace%bincsm.properties"

(FOR /f "usebackq tokens=1* delims==" %%i in ("%textFile%") do (
   if "%%i" equ "%search%" (
      echo %search%=%insert%%%j
   ) else if "%%j" neq "" (
      echo %%i=%%j
   ) else (
      echo %%i
   )
)) > temp.tmp

move /Y temp.tmp "%textFile%"

这篇关于使用批处理修改 .properties 文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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