【问题标题】:Find and Add a string to a text file using batch使用批处理查找字符串并将其添加到文本文件
【发布时间】:2014-09-30 22:00:15
【问题描述】:

如何搜索“描述=” 如果存在则什么都不做,但如果它不存在,则使用批处理脚本添加一个低于版本的行。

package.xml

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest>
  <Package name="audio"
           version="1.37.0"
           description="something.  . .."
           comment="">
   </Package>
</PackageManifest>

【问题讨论】:

    标签: batch-file search replace


    【解决方案1】:
    @ECHO OFF
    SETLOCAL
    DEL newfile.txt 2>nul
    SET "description=replacement text"
    SET "inserted="
    SET "version="
    FINDSTR /L /c:"description=" q25186276.txt >NUL
    IF ERRORLEVEL 1 (
     FOR /f "delims=" %%a IN (q25186276.txt) DO (
      ECHO(%%a
      FOR /f "tokens=1*delims== " %%b IN ("%%a") DO (
       IF "%%b"=="version" ECHO(     description="%description%"
      )
     )
    )>newfile.txt
    IF EXIST newfile.txt move /y newfile.txt q25186276.txt >NUL 2>nul
    
    GOTO :EOF
    

    我使用了一个名为 q25186276.txt 的文件,其中包含我的测试数据。

    您需要设置描述和缩进间距以适应。

    【讨论】:

    • 非常感谢您的帮助.. 救命稻草 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2021-11-25
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多