我正在嘗試制作一個將讀取電子表格的批處理檔案,并使用該作業表中的資料通過在檔案中的特定位置添加它們的內容來更新 HTML 檔案。為什么是電子表格?因為這樣我就可以將檔案名、目錄和文本存盤在一個地方添加,將其決議為一個陣列,然后按順序處理作業表以一次更新任意數量的檔案。
不幸的是,雖然讀取電子表格的代碼作業正常,但應該插入代碼的部分卻不是。它還以某種方式忽略了轉義字符。
這是我到目前為止所做的。
編輯:我遵循了這個建議“如果“!line!”=="%replaced%"",它沒有用。
@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
這將成功地重新創建我用于測驗的 HTML 檔案,但不會按預期插入包含在 %repalced% 中的行。它也沒有輸出空值,%replaced% 設定正確。我認為這if "%line%"=="%replace%"
沒有執行它的比較操作,但我不確定為什么會這樣。
此外,我最初使用注釋掉的代碼行作為標記而不是歷史記錄
,但由于某種原因,即使通過正確轉義字符,我也無法輸入感嘆號^^!
。
有任何想法嗎?或者甚至是一次將文本插入 1000 個檔案的其他/更好的方法?
編輯 2:對于那些詢問的人,這是我正在測驗的 HTML 檔案。由于我現在使用的 CSV 只是一行,所以就在這里。
testdir,infoboxtest,^<a href="https://www.youtube.com/watch?v=3-Gy4Zw1pGo"^>linkus injected^</a^>
我被告知我可能不需要轉義 csv 中的 > <。但是,由于根本沒有替換任何文本,并且從邏輯上講,腳本也只會鍵入轉義胡蘿卜,我認為這不是我的問題。
uj5u.com熱心網友回復:
@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=
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507903.html
標籤:批处理文件
上一篇:選擇腳本問題何時以及如何使用%1