-1

I'm trying to make a batch file that will read a spreadsheet, and use data from that sheet to update HTML files by adding to their contents at specific locations within the file. Why a spreadsheet? Because then I can store the file name, directory, and text to add all in one place, parse it into an array, and work through the sheet sequentially to update arbitrary numbers of files at once.

Unfortuantly, while the code reading the spreadsheet works fine, the part which should insert the code is not. It also, somehow, ignores escape characters.

Here's what I've done so far.

EDIT: I followed this suggestion "if "!line!"=="%replaced%"", it did not work.

@echo on
setlocal enableDelayedExpansion

cd /D "%~dp0data"

set /a heartoflorkan=0
for /F "tokens=1 delims=," %%a in ('Type "link.csv"') do (
    set /a heartoflorkan+=1
    set "outputa[!heartoflorkan!]=%%a"
)

For /L %%i in (1,1,%heartoflorkan%) Do (
    set "replaced=!outputa[%%i]!"
    call :writer
)
pause
goto:eof


:writer

cd /D "%~dp0"
set "replace=^<p class="AutoUpdateTag"^>History^<p^>"

set "source=infoboxtest"
set "target=text"

set flag=0

for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%.html') do (
      set "line=%%b"
      if "%line%"=="%replace%" (
        set "line=!replaced!"
        set flag=1
    )
    if defined line echo !line! >> %target%.tmp
    if "!flag!"=="1" (
        echo !replace! >> %target%.tmp
        set flag=0
    )
) 

del %source%.html
rename %target%.tmp %source%.html
goto:eof

This will successfully recreate the HTML file I'm using for testing, but will not insert the line contained within %repalced% as expected. It's not outputting a null either, %replaced% is being set correctly. I think that if "%line%"=="%replace%" isn't performing its comparison operation, but I'm not sure why that would be.

Additionally, I was originally using a commented-out line of code as the marker rather than History

, but for some reason, I couldn't get this to type an exclamation point, even when escaping the character correctly via ^^!.

Any ideas? Or even other/better ways to insert text into 1000s of files at a time?

Edit 2: For those who asked, here is the HTML file I am testing with. And since the CSV I am working with right now is just one line, here it is. testdir,infoboxtest,^<a href="https://www.youtube.com/watch?v=3-Gy4Zw1pGo"^>linkus injected^</a^>

I've been informed that I may not need to escape the > < in the csv. However, since no text at all is being replaced, and logically the script would just type the escape carrots out too, I do not think that is my problem.

  • Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) Specifically, since you're setting and using `%line%` inside of the same set of parentheses, you need to change the line to `if "!line!"=="%replaced%"`. – SomethingDark Sep 03 '22 at 17:54
  • I gave that a shot, and while it did not break the script, it still is not replacing the text. – Meep Changeling Sep 03 '22 at 17:59
  • My analysis of this convolution is that the contents of `link.csv` shuld be inserted before the line `

    History

    ` - is that correct? If not, please explain what the process is expected to do, together with a small "before" and "after" sample of data, AND explain what you mean by `It's not outputting a null either`

    – Magoo Sep 03 '22 at 18:40
  • @Magoo "My analysis of this convolution is that the contents of link.csv shuld be inserted before the line

    History

    - is that correct?" Yes, that is exactly what I am trying to do. By "It's not outputting a null either" I mean there's no blank line, null character, or space as I usually get when batch echos a unset variable to file.

    – Meep Changeling Sep 03 '22 at 18:49

1 Answers1

0
@ECHO OFF
SETLOCAL
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q73594275.txt"
SET "filename2=%sourcedir%\q73594275_2.txt"
SET "outfile=%destdir%\outfile.txt"

SET "targetstring=<p class="AutoUpdateTag">History<p>"

(
FOR /f "tokens=1*delims=]" %%b IN ('find /v /n "" ^<"%filename1%"') DO (
 IF "%%c"=="" (ECHO.) ELSE (
  IF "%%c"=="%targetstring%" TYPE "%filename2%"
  ECHO %%c
 )
)
)>"%outfile%"


GOTO :EOF

Always verify against a test directory before applying to real data.

Use find with /n to number each line that contains /v not nothing which simply numbers each line with a prefix [linenumber].

Read each line, assigning [linenumber to %%b and the real data (which may be empty) to %%c. If %%c is empty, echo an empty line. Otherwise, check whether it's the target. If so, insert the data from the second file; then echo the line regardless.

Obviously, outfile can't be the same as the filename1 or filename2.

Note that since the target line will remain in the output file, running the batch on the resulting file will continue to insert the required data over and over again.

Suggest that the output file is either in another directory or is in the source directory with another extension, given the plan to apply it to multiple files.

Magoo
  • 77,302
  • 8
  • 62
  • 84