SteffPoint
SteffPoint

Reputation: 559

Batch-File: Find string in a file and add text after

I generally use the echo method for writing "text" to an HTML-file by using:

  echo. >> "C:\Users\Me\Desktop\test.html"
  echo  ^<div^>TestDIV^</div^> >> "C:\Users\Me\Desktop\test.html"

I've experienced, that echo always writes the additional lines to the end of the target file.

But what if my target file (test.html) already has a structure like:

<div id="wrapperDIV">
    <div id="DIVcontent1">DIV 1</div>
    <div id="DIVcontent2">DIV 2</div>
</div>

and I want to write the additional lines within this existing structure, for example right after:

<div id="DIVcontent1">DIV 1</div>

I have tried to set up a script for this, but so far I couldn't make it running. (It seems to fail in the for-loop)

set DIVInput=^<div^>TestDIV^</div^>

set inputfile=C:\Users\Me\Desktop\test.html

(for /f usebackq^ delims^=^ eol^=  %%a in ("%inputfile%") do (

   if "%%~a"=="DIV 1^</div^>" call echo %DIVInput%
   echo %%a 
))>>"%inputfile%"

pause

Upvotes: 0

Views: 391

Answers (2)

Steve P
Steve P

Reputation: 1

I tried this code and it works, but blank lines are removed.

`enter code here`@echo off
`enter code here`setlocal 
`enter code here`set "DIVInput=^<div^>TestDIV^</div^>"
`enter code here`set inputfile=test.html
`enter code here`(for /f usebackq^ delims^=^ eol^=  %%a in ("%inputfile%") do (
   `enter code here`echo %%a 
   `enter code here`echo "%%~a" |find "DIV 1</div>" >nul && echo     %DIVInput%
`enter code here`))>"%inputfile%.new"
`enter code here`REM move /y "%inputfile%.new" "%inputfile%

Upvotes: 0

Stephan
Stephan

Reputation: 56155

Your set DIVInput=... consumes the escaping carets, which is bad in this case, because you need it later to echo the variable correctly. The preferred syntax set "var=value" preserves them (they are safe inside the quoting).

With your if line, you want to check for a substring only, not the whole line (if always compares complete strings and doesn't support wildcards). I used echo fullstring|find "string" to check that. Note that I use quotes (echo "%%~a"') for the same reason as above: proper handling of poison chars like>and<Same reason for not escaping with thefind` command ("save inside quotes")

You redirect to your input file. Redirecting is the first thing for the parser, so the file gets overwritten even before for has a chance to read the file. Use a different file and overwrite your original file when done.

You want to insert a string after a certain trigger line, but your code inserts before that line (well, that's easy to fix).

@echo off
setlocal 
set "DIVInput=^<div^>TestDIV^</div^>"
set inputfile=test.html
(for /f usebackq^ delims^=^ eol^=  %%a in ("%inputfile%") do (
   echo %%a 
   echo "%%~a" |find "DIV 1</div>" >nul && echo     %DIVInput%
))>"%inputfile%.new"
REM move /y "%inputfile%.new" "%inputfile%

Note: I disabled the move command for security reasons. Remove the REM after checking test.html.new looks ok.

Upvotes: 1

Related Questions