Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with file trimming in Batch

75 views
Skip to first unread message

John Stockton

unread,
Nov 30, 2021, 9:03:08 AM11/30/21
to
I want to copy an HTML page file to another file, omitting both the heading+introduction and the tailpiece. For test I use a line of four hashes as separator, and a test file :

head
####
want
####
tail

My non-working test code is in :

@echo PRUNE.BAT JRS 2021-11-30+ ???

@echo.
@if not exist zzz\nul mkdir zzz
@echo 1111

@SET ZC=0
@echo. Start > ZZZ\ORIG.HTM
@FOR /F "eol=; tokens=*" %%J IN (PRUNE.DAT) DO @(
@echo Comment line='%%J' ZC=%ZC%
if [%ZC%] == [1] (
echo '%%J' >> ZZZ\ORIG.HTM
)
if [%%J] == [####] (
set ZC=1
echo === %ZC%
)
@echo ++
)

@echo 9999

@echo. Ended >> ZZZ\ORIG.HTM
@echo.

type ZZZ\ORIG.HTM

@echo PRUNE.BAT ends.

The apparent problem is that the line set ZC=1 has no effect, so that the wanted part is not copied. ( I intend to change it to set /A ZC=1+%ZC% which should mean that the tail part is not copied). Subsequently I may use a line   as an invisible separator.

What's wrong?


--
(c) John Stockton, near London, UK. Using Google Groups. |

LangerTom

unread,
Nov 30, 2021, 1:24:13 PM11/30/21
to
I believe that your command
SET ZC=1
is working fine, but checking its result with
echo === %ZC%
is going wrong because both lines are placed between round brackets.
CMD.EXE is reading all code betwwen round brackets at once and is
resolving variables like %ZC% during code reading. At this time your
command
set ZC=1
did not take place yet.
To avoid this switch on DELAYEDEXPANSION and note variables like this; !ZC!

The following code should work (not tested):

HTH Thomas

By the way: working on html with batch is very hard stuff because html
code contains many poison characters like <>?*/"&

<code>

@echo.
SETLOCAL ENABLEDELAYEDEXPANSION
@if not exist zzz\nul mkdir zzz
@echo 1111

@SET ZC=0
@echo. Start > ZZZ\ORIG.HTM
@FOR /F "eol=; tokens=*" %%J IN (PRUNE.DAT) DO @(
@echo Comment line='%%J' ZC=!ZC!
if [!ZC!] == [1] (
echo '%%J' >> ZZZ\ORIG.HTM
)
if [%%J] == [####] (
set ZC=1
echo === !ZC!
)
@echo ++
)

@echo 9999

@echo. Ended >> ZZZ\ORIG.HTM
@echo.

type ZZZ\ORIG.HTM

@echo PRUNE.BAT ends.


</code>

JJ

unread,
Dec 1, 2021, 2:04:33 AM12/1/21
to
The easiest way is to use Delayed Expansion as other have mentioned, but it
can produce more severe problem if the source data includes `!` character.

Without Delayed Expansion, move the code that use variables which are
updated within the same or parent command groups, into a subroutine. e.g. in
your case, change it like below.

@echo PRUNE.BAT JRS 2021-11-30+ ???

@echo.
@if not exist zzz\nul mkdir zzz
@echo 1111

@SET ZC=0
@echo. Start > ZZZ\ORIG.HTM
@FOR /F "eol=; tokens=*" %%J IN (PRUNE.DAT) DO @(
@echo Comment line='%%J' ZC=%ZC%
if [%ZC%] == [1] (
echo '%%J' >> ZZZ\ORIG.HTM
)
if [%%J] == [####] (
set ZC=1
call :DispZC
)
@echo ++
)

@echo 9999

@echo. Ended >> ZZZ\ORIG.HTM
@echo.

type ZZZ\ORIG.HTM

@echo PRUNE.BAT ends.
goto :eof

:DispZC
echo === %ZC%

John Stockton

unread,
Dec 1, 2021, 6:35:19 PM12/1/21
to
On Tuesday, 30 November 2021 at 18:24:13 UTC, LangerTom wrote:
> I believe that your command
> SET ZC=1
> is working fine, but checking its result with
> echo === %ZC%
> is going wrong because both lines are placed between round brackets.

Not actually wrong, as it shows that %ZC% is not returning what I want;
and so explains why the next line, if [%ZC%] == [1] ( , is not doing
what I wanted it to do <g>.

> CMD.EXE is reading all code between round brackets at once and is
> resolving variables like %ZC% during code reading. At this time your
> command
> set ZC=1
> did not take place yet.
> To avoid this switch on DELAYEDEXPANSION and note variables like this; !ZC!
>
> The following code should work (not tested):

It does. I copied the whole lot, and it ran. All that I now want is to arrange that the second #### sets ZC to something other than 1, which should be easy (P.S. it was : set /a ZC=1-!ZC!). And I don't want the #### marks to appear in the output as rendered on the screen, but using &nbsp; instead should serve.

I control the input file; I can alter its content in any way that does not adversely affect what appears on the screen when the HTML is rendered. The present part, a pre-processor, just copies the HTML unchanged (I hope), but omitting blocks that I do not want in the output.

> HTH Thomas
>
> By the way: working on html with batch is very hard stuff because html
> code contains many poison characters like <>?*/"&

Understood. No problem here. It is the code which runs next that handles the HTML; it copies the wanted lines, which are those containing the name-strings provided as arguments, and separately lists the contents of the first href=" ... "> in each of those lines for XCOPYing as in my previous thread here. As the input is HTML, if there is a second such link on one of those lines line I will just insert a newline in the input - I think.

Thanks again! |
0 new messages