简体   繁体   中英

Find and Add a string to a text file using batch

How can i search for "description=" if exists do nothing but if it doesn't exists add a under version line using batch script.

package.xml

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest>
  <Package name="audio"
           version="1.37.0"
           description="something.  . .."
           comment="">
   </Package>
</PackageManifest>
@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

I used a file named q25186276.txt containing your data for my testing.

You would need to set the description and indent-spacing to suit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM