Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

cmd - Modify a string in a .properties file with batch

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
        )
    )
)

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.

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

After modifications, CLASSPATHshould look like this:

CLASSPATH=plugins/Numbering.jar:lib/patch.jar:lib/brand_api.jar
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simpler...

@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%"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...