使用 DOS 批处理脚本:在属性文件中查找一行并替换文本

发布于 2024-12-05 18:01:44 字数 407 浏览 0 评论 0原文

尝试使用批处理文件替换属性文件中某一行的字符串。我知道这可以在不使用临时文件的情况下完成,正如我以前见过的那样,但忘记了如何做到这一点。

我知道,如果我有一个包含以下内容的 var.properties 文件:

CLASSPATH=bsh.jar;other.jar
VARTEST=dummy
ANOTHERVAR=default

我正在尝试更新 .properties 文件中的 CLASSPATH 值,而不更改属性文件的顺序。

这是一个属性文件,所以我相信答案与使用有关:

for /f "tokens=1,2* delims==" %%i in (var.properties) do (
  @echo Key=%%i Val=%%j
)

Trying to replace a string in a properties file on a certain line by using a batch file. I know that this can be done WITHOUT the use of a temp file, as I have seen it before, but forgotten how to do it.

I know that if I have a var.properties file that contains this:

CLASSPATH=bsh.jar;other.jar
VARTEST=dummy
ANOTHERVAR=default

I am trying to update the CLASSPATH value in the .properties file without changing the order of the properties file.

This is a properties file and so I believe the answer would be related to using:

for /f "tokens=1,2* delims==" %%i in (var.properties) do (
  @echo Key=%%i Val=%%j
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-12-12 18:01:44

使用 find 代替 findstr,并在“classpath”上使用 /v/i 开关。这将省略返回包含类路径的行,然后您可以沿着 w/VARTEST=dummy 回显文件中所需的内容

SET NEWVAL=CLASSPATH=test.jar
SET FILE=think.properties
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%" ^|FIND /V /I "classpath"`) DO (
 ECHO CLASSPATH=%NEWVAL%>>"%FILE%"
 ECHO %%A>>"%FILE%"
)

编辑:

SETLOCAL ENABLEDELAYEDEXPANSION
SET NEWVAL=test.jar
SET OLDFILE=OLD_think.properties
SET NEWFILE=think.properties
SET COUNT=1

MOVE "%NEWFILE%" "%OLDFILE%"

FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%OLDFILE%" ^|FIND /C /I "classpath"`) DO (
 SET LINE=%%A
 )
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%OLDFILE%"`) DO (
 IF %COUNT% NEQ %LINE% (ECHO %%A>>"%NEWFILE%") ELSE (ECHO %NEWVAL%>>"%NEWFILE%")
 SET /a COUNT=!COUNT!+1
)

基本上说明,

  1. 将 think.properties 重命名为 OLD_think.properties
  2. 读取 OLD_think。属性并找到带有字符串的行号
    并将其设置为变量 LINE
  3. 查找 OLD_think.properties 中的所有行,并将它们回显到 think.properties 中。一旦到达“CLASSPATH”字符串所在的行,它就会插入您想要替换它的新行,其他所有内容都保持不变。

Instead of findstr use find with the /v and /i switches on "classpath". This will OMIT returning the line with classpath in it, then you can echo what you want in the file along w/VARTEST=dummy

SET NEWVAL=CLASSPATH=test.jar
SET FILE=think.properties
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%" ^|FIND /V /I "classpath"`) DO (
 ECHO CLASSPATH=%NEWVAL%>>"%FILE%"
 ECHO %%A>>"%FILE%"
)

EDIT:

SETLOCAL ENABLEDELAYEDEXPANSION
SET NEWVAL=test.jar
SET OLDFILE=OLD_think.properties
SET NEWFILE=think.properties
SET COUNT=1

MOVE "%NEWFILE%" "%OLDFILE%"

FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%OLDFILE%" ^|FIND /C /I "classpath"`) DO (
 SET LINE=%%A
 )
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%OLDFILE%"`) DO (
 IF %COUNT% NEQ %LINE% (ECHO %%A>>"%NEWFILE%") ELSE (ECHO %NEWVAL%>>"%NEWFILE%")
 SET /a COUNT=!COUNT!+1
)

Basically states,

  1. rename think.properties to OLD_think.properties
  2. read OLD_think.properties and find the line number with string
    "classpath" in it and set it to variable LINE
  3. Find all lines in OLD_think.properties, and echo them into think.properties. once you reach the line where your "CLASSPATH" string existed, it inserts the new line you wanted to replace it with, and everything else stays the same.
眼泪也成诗 2024-12-12 18:01:44

我终于崩溃并接受了使用“临时”文件的方法。使用带有“!”的延迟扩展性格解决了我的问题。这一成功很大程度上归功于 mecaflash 的投入。

您可以使用以下命令调用此脚本:CALL Script.bat PropKey NewPropValue Filename

@echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
  ECHO Script will optionally accept 3 args: PropKey PropVal File
  SET PROPKEY=UseCompression
  SET PROPVAL=false
  SET FILE=config.properties
) ELSE (
  SET PROPKEY=%1
  SET PROPVAL=%2
  SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
  SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
  IF "!COUNT!" NEQ "%LINE%" (
      ECHO %%A>>"%FILE%"
  ) ELSE (
      ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
      ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
  )
  SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end

I finally broke down and accepted a method using a "temp" file. Using delayed expansion with the '!' character solved my question. Much of this success was due to input by mecaflash .

You can call this script with: CALL Script.bat PropKey NewPropValue Filename

@echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
  ECHO Script will optionally accept 3 args: PropKey PropVal File
  SET PROPKEY=UseCompression
  SET PROPVAL=false
  SET FILE=config.properties
) ELSE (
  SET PROPKEY=%1
  SET PROPVAL=%2
  SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
  SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
  IF "!COUNT!" NEQ "%LINE%" (
      ECHO %%A>>"%FILE%"
  ) ELSE (
      ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
      ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
  )
  SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文