error on for /f in the block

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

error on for /f in the block

#1 Post by einstein1969 » 01 Nov 2022 09:00

Hello,

I need help with a for /f

if I run this code, the for /f gives me an error

Code: Select all

@echo off & setlocal enableDelayedExpansion

>"%TMP%\env.txt" set

set /a nl=10
(
for /f "skip=!nl! eol== delims==" %%V in (%TMP%\env.txt) do set "%%~V="
)

Code: Select all

!nl! eol== delims==" non atteso.
the for is inside a block, can someone explain to me how to solve it and why it don't works?

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: error on for /f in the block

#2 Post by miskox » 01 Nov 2022 10:10

If I replace

Code: Select all

!nl!
with

Code: Select all

%nl%
it works.

Why? Maybe % should not be replaced by ! (%%a is not replaced by !!a for example) in the command itself.

Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: error on for /f in the block

#3 Post by aGerman » 01 Nov 2022 13:57

If you really need the delayed expansion of n1 then your best bet might be to run the loop in a subroutine. Another approach could be processing the output of MORE +!n1! "filename".

Steffen

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: error on for /f in the block

#4 Post by einstein1969 » 02 Nov 2022 05:46

thanks

i use "more +!nl!" is more efficient

Post Reply