![如何使用 Windows CMD 在目錄及其子目錄中的多個 html 檔案中搜尋和取代字串?](https://rvso.com/image/1520725/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Windows%20CMD%20%E5%9C%A8%E7%9B%AE%E9%8C%84%E5%8F%8A%E5%85%B6%E5%AD%90%E7%9B%AE%E9%8C%84%E4%B8%AD%E7%9A%84%E5%A4%9A%E5%80%8B%20html%20%E6%AA%94%E6%A1%88%E4%B8%AD%E6%90%9C%E5%B0%8B%E5%92%8C%E5%8F%96%E4%BB%A3%E5%AD%97%E4%B8%B2%EF%BC%9F.png)
這要求類似於如何使用 Windows CMD 搜尋並取代多個文字檔案(目錄內)中的字串?第2部分
我想對資料夾及其子資料夾內的所有 HTML 檔案執行查找和替換。
在這裡,由於某種原因,我從其中獲取 2 個輸入,其中 1 個是檔案路徑,另一個是該檔案路徑中的資料夾名稱。
set /p INPUT1= Enter Source file path:
set /p INPUT2= Enter Source folder name (**Without any Space**):
Here let's assume (below folders can change):
INPUT1= D:\!Test\
INPUT2= Folder1
Then path is D:\!Test\Folder1\
Inside Folder1 there are some more folders like:
....Folder1\A\Common\
....Folder1\B\Common\
....Folder1\C\Common\
In above folder **A** as well as folder **Common** have html files.
I have coded this in .cmd file as below:
set /p INPUT1= Enter Source file path:
set /p INPUT2= Enter Source folder name (**Without any Space**):
REM 將資料夾中的文字 FindTextX.html 替換為 ReplaceTextA.htmlA, ReplaceTextB.html 資料夾中乙, ReplaceTextC.html 資料夾中C。
設定“OldStringA=FindTextX.html” 設定“NewStringA=ReplaceTextA.html” 設定“NewStringB=ReplaceTextB.html” 設定“NewStringC=ReplaceTextC.html”
@ECHO OFF &SETLOCAL
cd %INPUT1%\%INPUT2%\A\
for %%x in (*.html) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldStringA%=%NewStringA%!"
)
echo(!ln!
endlocal
))>"%outFile%"
REM exit /b
REM Come out of A folder
cd..
REM Go inside of B folder
cd B
for %%x in (*.html) do call:process1 "%%~x"
goto:eof
:process1
set "outFile=%~n1%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldStringB%=%NewStringB%!"
)
echo(!ln!
endlocal
))>"%outFile%"
.......So on for folder **C** .....
exit /b
然而這不起作用。我在資料夾內得到空的 html 文件A,乙&C。但是它不適用於子資料夾常見的它位於每個 A、B 和 C 資料夾內。我希望這個腳本替換資料夾下 HTML 文件中的文本A,乙和C以及其中的文字子資料夾。 注意:我不需要任何額外的 BAT 文件,例如 FART、REPLACER、.... 等。 感謝您的協助。