Exit Equal Sign in DOS String Replacement Command

I need to replace some text in a JNLP file using a DOS batch file to configure it for a local machine.

The problem is that the search pattern contains an equal sign, which ruined the replacement string in the batch file.

I want to replace the string,

<j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/> 

with specific settings for the size of the initial and maximum heap.

For example, at the moment I have

 for /f "tokens=* delims=" %%a in (%filePath%agility.jnlp) do ( set str=%%a set str=!str:initial-heap-size="100M"=initial-heap-size="%min%M"! echo !str!>>%filePath%new.jnlp) 

but = in the search pattern is read as part of the replacement command.

How can I avoid the equal sign, so it is treated as text?

+2
source share
4 answers

Here is an alternative solution. If you can afford to download GNU tools, you can use sed :

 C:\test>set a=200 C:\test>sed -i.bak "s/^\(.*initial-heap-size=\"\).*\( max.*\)/\1%a%\"\2/" file 
-3
source

The best solution is to download and install Cygwin or GNUWin32 , but if you are really limited by the standard shell, it might get a little confused.

This is not the fastest method in the world, but it is at least functional. This batch file processes each line one character at a time, especially considering the case when you find the line you are looking for.

 @echo off set init=50M set max=75M setlocal enableextensions enabledelayedexpansion for /f "tokens=* delims=" %%a in (agility.jnlp) do ( set str1=%%a call :morph echo !str2!>>agility_new.jnlp echo !str2! ) endlocal goto :eof :morph set str2= :morph1 if not "x!str1!"=="x" ( if "!str1:~0,18!"=="initial-heap-size=" ( set str2=!str2!initial-heap-size="!init!" set str1=!str1:~24! goto :morph1 ) if "!str1:~0,14!"=="max-heap-size=" ( set str2=!str2!max-heap-size="!max!" set str1=!str1:~20! goto :morph1 ) set str2=!str2!!str1:~0,1! set str1=!str1:~1! goto :morph1 ) goto :eof 

With input file:

 <j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/> next line === 

in the end it will turn out:

 <j2se version="1.5" initial-heap-size="50M" max-heap-size="75M"/> next line === 
+1
source

You cannot just replace (a substring with) an equal sign without breaking (for-statement with "delims==" ) or trimming ...

But perhaps you could go for this simpler but more confusing approach using the following statement in your for-loop:

 set str=!str:"100M" max-heap-size="%min%M" max-heap-size! 

It simply combines the replacement string with what comes after , rather than what comes before , completely eliminating any equivalent replacements.

+1
source

If you can pass arguments as something else, such as double underscores, you can iterate over them and convert them to '=' in a batch file.

 @rem Replace __ with = in batch files. @rem This works around the lack of equals signs in args @rem args contains full args string with substitutions in place setlocal enabledelayedexpansion :argloop if "%~1" NEQ "" ( set str=%~1 set out=!str:__==! set %~1=!out! set args=!args!!out! SHIFT goto :argloop ) @rem Can now run program on a line on its own with just %args% 

Source: https://github.com/mlabbe/batchargs

0
source

All Articles