html - 批处理 - 找到匹配字符串时拆分 html 文件

标签 html batch-file

我有 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

最佳答案

  1. 删除第二行,它禁用了 ! 变量。
  2. 要在 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/

相关文章:

jquery - 将表格中的第 th 个元素垂直居中

cmd - 我可以使用 FOR/F 处理文件和命令、文件和字符串或几个文件,但不能使用其他组合。为什么?

batch-file - 用于重命名然后将文件从 1 个文件夹移动到另一个文件夹的批处理脚本

javascript - 使用 javascript 在选择列表中选择的下一个日期

batch-file - 错误 : "|" was unexpected at this time batch script

windows - 设置环境变量

batch-file - 批处理语句中 > nul 2>&1 是什么意思

java - 通过html或java获取系统信息?

html - 在 CSS 3 中,为什么使用 `@keyframes` 在这里不起作用?

html - 3个DIV彼此相邻,中间填充空白