Discussion:
Problem with file trimming in Batch
(too old to reply)
John Stockton
2021-11-30 14:03:07 UTC
Permalink
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. |
JJ
2021-12-01 07:04:29 UTC
Permalink
Post by John Stockton
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
head
####
want
####
tail
@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?
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
2021-12-01 23:35:18 UTC
Permalink
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!
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! |
--
(c) John Stockton, near London, UK. Using Google Groups. |
Loading...