-1

I have a text file:

#deployment.properties
#Thu Dec 22 11:08:21 PST 2016
deployment.modified.timestamp=1482433701009
deployment.roaming.profile=false
deployment.version=8
install.disable.sponsor.offers=false
deployment.cache.enabled=false
deployment.javaws.viewer.bounds=480,186,720,360
deployment.browser.path=C\:\\Program Files\\Internet Explorer\\iexplore.exe
#Java Deployment jre's
#Thu Dec 22 11:08:21 PST 2016
deployment.javaws.jre.1.location=http\://java.sun.com/products/autodl/j2se
deployment.javaws.jre.0.args=
deployment.javaws.jre.0.registered=true
deployment.javaws.jre.1.args=
deployment.javaws.jre.1.enabled=false
deployment.javaws.jre.1.registered=true
deployment.javaws.jre.0.osarch=x86
deployment.javaws.jre.0.osname=Windows
deployment.javaws.jre.1.product=1.6.0_45
deployment.javaws.jre.0.platform=1.8
deployment.javaws.jre.0.path=C\:\\Program Files (x86)\\Java\\jre1.8.0_111\\bin\\javaw.exe
deployment.javaws.jre.0.location=http\://java.sun.com/products/autodl/j2se
deployment.javaws.jre.1.path=C\:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe
deployment.javaws.jre.0.enabled=true
deployment.javaws.jre.1.osarch=x86
deployment.javaws.jre.1.osname=Windows
deployment.javaws.jre.1.platform=1.6
deployment.javaws.jre.0.product=1.8.0_111

I need to find the line which contains C\:\\Program Files (x86)\\Java\\jre1.8.0_111\\bin\\javaw.exe.

On this line I can see that it is .jre.0.enabed =. The 0 is important to me because this means it is the first jre. So now I know 0 means Java 8 and 1 means Java 6. So now I need to go to Java 8 enable property and mark it as true and go to Java 6 enabled property and mark it as false.

I want to do this in a batch file but I am very new to batching. I have found some code to search for lines and modify them, but I need to search for a line here:

C\:\\Program Files (x86)\\Java\\jre1.8.0_111\\bin\\javaw.exe.

And then move back and see what number comes after the jre. There could also be more than 2 jre so these number can go say even have 3 (0, 1, 2) etc.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Do you need to change something in the file? The line can be easy filtered `type java.properties| find "jre1.8.0_111\bin\javaw.exe"` – npocmaka Dec 22 '16 at 19:55
  • @npocmaka Yes I need to make sure deployment.javaws.jre.0.enabled=true and all other deployment.javaws.jre.*.enabled areequal to false – user4264164 Dec 22 '16 at 20:29
  • 1
    take a look here - http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – npocmaka Dec 22 '16 at 20:39
  • @npocmaka I looked, but I can't add any third party dependencies. The systems this will be running on are not governed by me. – user4264164 Dec 22 '16 at 20:46
  • there are answers that does not require third party tools. Anyway you are going to write a script I think you can use another script that does not require third party binaries. As it is a script everybody can check its content and check it it is harmful. – npocmaka Dec 22 '16 at 20:50
  • 1
    Please note that https://stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – DavidPostill Dec 22 '16 at 21:39
  • If I got it right, there is one `*.jre.?.enabled` line with the same number at `?` as the `*.jre.0.path` line that holds the search string, is that correct? So to find a certain line, use [`findstr`](http://ss64.com/nt/findstr.html)`/C:"literal search string"`; to capture its output, follow [this post](http://stackoverflow.com/q/16203629); to get the number after `*.jre.`, take a look at [this post](http://stackoverflow.com/a/41293983)... – aschipfl Dec 23 '16 at 11:10
  • ...you can build a search string for another `findstr` command to find the applicable `*.jre.?.enabled=*` part, then use [`for /F`](http://ss64.com/nt/for_f.html) to split off the string portion after the `=` sign... – aschipfl Dec 23 '16 at 11:11

1 Answers1

0
@echo off
setlocal EnableDelayedExpansion

rem   Search for the first ID of '*.product=1.8.*'
rem   and get ID and JRE version
set "ID="
set "JRE="
for /f "usebackq tokens=*" %%A in (`findstr /c:".product=1.8." "config.txt"`) do (
    if not defined ID (
        set "str=%%A"
        rem   Remove '*.product' from the beginning of str
        set "JRE=!str:*.product=!"
        rem   Remove first symbol '=' from JRE
        set "JRE=!JRE:~1!"
        rem   Remove '*.jre.' from the beginning of str
        set "ID=!str:*.jre.=!"
        rem   Remove JRE from ID
        call set "ID=%%ID:!JRE!=%%"
        rem   Remove last symbol '=' from ID
        set "ID=!ID:~0,-1!"
        rem   Remove '.product' from ID
        set "ID=!ID:.product=!"
    )
)

rem   Enable jre with found ID and disable others
for /f "usebackq tokens=*" %%A in ("config.txt") do (
    call :process_line "%%A"
)

endlocal
exit /b 0

:process_line
set "str=%~1"

rem   If line does not contain '.jre.' skip it
set "notJre=%str:.jre.=%"
if "%str%"=="%notJre%" goto process_line_skip

rem   If line does not contain '.jre.*.enabled' skip it
set "notEnabled=%str:.enabled=%"
if "%str%"=="%notEnabled%" goto process_line_skip

rem   Enable/disable jre
call set "notID=%%str:.jre.%ID%.=%%"
if "%str%"=="%notID%" (call :process_line_enabled false) else (call :process_line_enabled true)
exit /b 0

:process_line_skip
echo %str%
exit /b 0

:process_line_enabled
for /f "tokens=1,2 delims==" %%B in ("%str%") do echo %%B=%1
exit /b 0
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35