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 ^>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.
CodePudding user response:
@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=