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%bin\csm.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, CLASSPATH
should look like this:
CLASSPATH=plugins/Numbering.jar\:lib/patch.jar\:lib/brand_api.jar
Simpler...
@echo OFF
setlocal
set "search=CLASSPATH"
set "insert=plugins/Numbering.jar\:"
set "textFile=%workingPlace%bin\csm.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%"
You can give this a go:
@echo off
setlocal enableextensions disabledelayedexpansion
set "replace=plugins^/Numbering.jar^\^:"
set "textFile=%workingPlace%bin\csm.properties""
for /f %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
for /f "tokens=1* delims==" %%a in ("%%i") do (
if "%%a"=="CLASSPATH" (
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line!%replace%
) else (
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line!
endlocal
)
)
)
Similar theory, it will only replace the full string if the first token (%%i) matches CLASSMAP
Please do not change the double quotations in the set
commands.
Here is my solution for this string replacement task using only internal commands of cmd.exe
with exception of FINDSTR.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not defined workingPlace set "workingPlace=%~dp0"
set "TextFile=%workingPlace%bin\csm.properties"
if not exist "%TextFile%" goto EndBatch
set "TempFile=%TEMP%\csm.properties.tmp"
set "FoundInfo="
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%TextFile%"') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
set "Line=!Line:*:=!"
if not defined FoundInfo (
if defined Line (
if /I "!Line:~0,10!" == "CLASSPATH=" (
if /I "!Line!" == "CLASSPATH=" (
echo !Line!plugins/Numbering.jar
endlocal
set "FoundInfo=1"
) else if "!Line:plugins/Numbering.jar=!" == "!Line!" (
set "Line=!Line:~0,10!plugins/Numbering.jar\:!Line:~10!"
echo !Line!
endlocal
set "FoundInfo=1"
) else (
endlocal
goto DeleteTempFile
)
) else (
echo(!Line!
endlocal
)
) else (
echo/
endlocal
)
) else (
echo(!Line!
endlocal
)
))>"%TempFile%"
if not defined FoundInfo echo CLASSPATH=plugins/Numbering.jar>>"%TempFile%"
move /Y "%TempFile%" "%TextFile%"
:DeleteTempFile
if exist "%TempFile%" del "%TempFile%"
:EndBatch
endlocal
Read my answer on How to read and print contents of text file line by line? why command FINDSTR is used just to output every line in file csm.properties
including empty lines ignored by FOR by default with line number and :
to avoid that any line is ignored by FOR. The line number and the colon is removed by the command line set "Line=!Line:*:=!"
.
There is the environment variable FoundInfo
undefined at top of the batch file and which is set once a line starting case-insensitive with CLASSSPATH=
is processed by the inner code of FOR loop. Every line in file after the line starting with CLASSSPATH=
is just output without further processing including empty lines.
An empty line above line starting with CLASSSPATH=
is also output with echo/
without any further processing.
The first line starting case-insensitive with CLASSPATH=
can be processed in three different ways:
- The line contains just
CLASSPATH=
.
In this case the line is output as CLASSPATH=plugins/Numbering.jar
and that's it.
- The line starts with
CLASSPATH=
and contains one or more characters, but not case-insensitive the string plugins/Numbering.jar
.
In this case the line is output within inserting plugins/Numbering.jar\:
after CLASSPATH=
.
Please note that a line with just CLASSPATH=
and one or more trailing spaces/tabs would result also in running into second branch resulting for example in output of CLASSPATH=plugins/Numbering.jar\:
with \:
and the trailing whitespaces at end.
- The line starts with
CLASSPATH=
and contains already case-insensitive the string plugins/Numbering.jar
somewhere on the line.
In this case the FOR loop is exited immediately with a jump to label DeleteTempFile
without processing any further line from captured output of FINDSTR. So the last modification date of the file does not change because of nothing changed on file content. (I don't like a change last modification date on file content not really modified.)
After the FOR loop is checked if there was any line starting case-insensitive with CLASSPATH=
at all in the file. The line CLASSPATH=plugins/Numbering.jar
is appended to the temporary file if that was not the case.
Finally with temporary file definitely being different to csm.properties
, the temporary file is moved over existing file csm.properties
if that is possible at all and last the temporary file is deleted if it is still existing.
Note 1: The solution could be easier without usage of FINDSTR if file csm.properties
contains no empty lines or it is acceptable that empty lines are removed during the update of line with CLASSPATH=
.
Note 2: The line with CLASSPATH=
at top of file csm.properties
reduces the process time.
Summary of features of this solution:
- Does not modify the text file on containing already
CLASSPATH=
with plugins/Numbering.jar
somewhere on line.
- Inserts
plugins/Numbering.jar\:
after CLASSPATH=
only if there are other class paths (or trailing whitespaces) on this line.
- Appends
plugins/Numbering.jar
to existing CLASSPATH=
line not containing any other class path (and no trailing whitespaces on this line).
- Appends entire
CLASSPATH=
line with plugins/Numbering.jar
to file not containing this line at all if the file exists at least.
- Keeps empty lines in text file and so modifies really only line with
CLASSPATH=
at beginning.
- Does not modify lines with
VARIABLE==value
(value with equal sign at beginning) to VARIABLE=value
(equal sign at beginning removed).
- Does not modify spelling of
CLASSPATH=
and works for that reason also with classpath=
or ClassPath=
in file.
- Does not remove lines starting with
;
being default of FOR's end of line option (eol).
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
move /?
set /?
setlocal /?
See also Why is no string output with 'echo %var%' after using 'set var = text' on command line? and How to set environment variables with spaces? These answers explain why in most cases set variable="value"
is not good and what is the difference to set "variable=value"
which is the preferred syntax for definition of an environment variable with a string value.
See also Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files explaining how string comparison works with command IF and why the operators EQU
and NEQ
designed primary for integer comparisons should be in general not used for comparing two strings although this is possible. The usage of EQU
and NEQ
for string comparisons can in some cases with not double quoted strings result in an unexpected comparison result.
@echo off
setlocal disabledelayedexpansion
set "search=CLASSPATH"
set "insert=plugins/Numbering.jar\:"
set "textFile=%workingPlace%bin\csm.properties"
for /f "usebackq delims=" %%i in ("%textFile%") do (
for /f "tokens=1* delims==" %%g in ("%%i") do (
if /i "%%g" == "%search%" (
set "token1=%%g"
set "token2=%%h"
setlocal enabledelayedexpansion
>> "%textFile%.tmp" echo(!search!=!insert!!token2!
endlocal
) else (
set "line=%%i"
setlocal enabledelayedexpansion
>> "%textFile%.tmp" echo(!line!
endlocal
)
)
)
move "%textFile%" "%textFile%.bak" && move "%textFile%.tmp" "%textFile%"
Insert seems needed rather than a replace.
The for loop reads each line of %textfile%
and the nested for
loop
delimits on the =
to store token 1 and token 2 with the remainder.
If %search%
is found, then !token1!
and !token2!
is set
the
token values. set
usually can handle poison characters after
it so why it is used. Expansion is delayed so poison characters
are echoed to file without being expanded into the source.
If %search%
is not found, the line is set to !line!
,
expansion is delayed, and then the line is echoed to file.
Note:
%workingPlace%
is unknown so correct the path as needed.