我有 1 个 html 文件,其中包含 2 个单独的 html 文件。我想写一个批处理文件来查找标签,并将原始 html 拆分为 2 个 html 文件。
这是我尝试过的
@echo off & setlocal enabledelayedexpansion
SETLOCAL DisableDelayedExpansion
set c=0
for /f "tokens=*" %%a in (split.html) do (
if "%%a" equ "</html>" (
set /a c+=1
>f!c!.html echo.
) else (
>> f!c!.html echo( %%a)
)
)
在 html 文件中,有 !
字符,但在输出文件中它们都丢失了。如何在输出文件中保留 !
?
谢谢
更新 26-07-2015 我从这里得到了答案。感谢大家的帮助。 Split text file into 2 files by separator
@echo off
set /p file=FILE TO PROCESS :
del /q /f out.file*
setlocal enableDelayedExpansion
set out_file_counter=1
for /f "usebackq delims=" %%L in ("%file%") do (
set line=%%L
if "!line:~0,5!" equ "=====" (
set /a out_file_counter=out_file_counter+1
) else (
echo !line!>>out.file.!out_file_counter!
)
)
endlocal
最佳答案
- 删除第二行,它禁用了
!
变量。 要在 html 中正确输出
!
字符,请禁用循环内的扩展:@echo off & setlocal enabledelayedexpansion set c=0 for /f "tokens=*" %%a in (split.html) do ( if "%%a" equ "</html>" ( set /a c+=1 >f!c!.html echo. ) else ( >> f!c!.html ( ENDLOCAL echo %%a SETLOCAL EnableDelayedExpansion ) ) )
关于html - 批处理 - 找到匹配字符串时拆分 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31619795/