批处理文件 - 读取文件,获取特定行,解析该行,然后更改某些行

时间:2016-12-22 19:22:19

标签: windows batch-file scripting

我有一个文本文件:

#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

我需要找到包含C\:\\Program Files (x86)\\Java\\jre1.8.0_111\\bin\\javaw.exe的行。

在这一行,我可以看到它是.jre.0.enabed =0对我很重要,因为这意味着它是第一个jre。所以现在我知道0表示Java 8而1表示Java 6.所以现在我需要转到Java 8 enable属性并将其标记为true并转到Java 6启用属性并将其标记为false

我想在批处理文件中执行此操作,但我对批处理非常新。我找到了一些代码来搜索行并修改它们,但我需要在这里搜索一行:

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

然后返回查看jre之后的数字。也可能有超过2个jre,所以这些数字可以说甚至有3个(012)等。

1 个答案:

答案 0 :(得分: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