2

I have 1 html file which contains 2 separate html file in it. I want to write a batch file that look for tag , and split original html in 2 html files.

This is what I tried

@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)
)
)

In html file, there's ! character, but in output file all of them are missing. How do keep ! in the output file?

Thanks

Update 26-07-2015 I got answer from here. Thank you all for your help. 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
Lam
  • 429
  • 6
  • 12

3 Answers3

0
  1. Remove the second line, it disabled ! variables.
  2. To correctly output ! characters inside html disable the expansion inside the loop:

    @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
            )
        )
    )
    
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • In html file there is `!` character, but the output is missing all of them. I think it's `enabledelayedexpansion ` – Lam Jul 25 '15 at 21:12
  • Well, you can disable it inside the loop, see the updated the answer. – wOxxOm Jul 25 '15 at 21:50
0
@echo off
setlocal
call :SplitFile < split.html > split-1.html
goto :EOF

:SplitFile
   set "line="
   set /P "line="
   set /P "=%line%" < NUL
   echo/
   if "%line%" equ "</html>" (
      rem Copy the rest of lines to part 2 and terminate
      findstr "^" > split-2.html
      exit /B
   )
goto SplitFile
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I got error at line 3 `The syntax of the command is incorrect`. Thanks – Lam Jul 25 '15 at 21:10
  • Please, insert a `REM` before the first `@echo off`, run the Batch file and post _the whole line_ that appear _below_ the error messsage. As far as I know, Batch file errors are _not_ reported with line numbers... – Aacini Jul 25 '15 at 23:52
  • The bat just stopped at line 3. Here's error message http://prntscr.com/7x3khf. I have used your latest version – Lam Jul 26 '15 at 07:35
  • I tested my code and it works correctly. The problem you reported is _very unsual_. Perhaps you may be interested in know what is happening here, because you may get a similar problem afterward. Note: in batch files the error message appear _first_ and _below it_ appear the offending line. This is the reason because I say that your problem is _very unusual_! – Aacini Jul 26 '15 at 15:04
0

I got answer from here. Thank you all for your help. 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
Lam
  • 429
  • 6
  • 12