Script package "for / f ..."

for /F "skip=n tokens=3 delims= " %%i in (myfile.txt) do echo %%i

Is it possible that there skip=n ...will be a variable of the type skip=%test% ...where it %test%has an integer value?

So, I'm trying to add a data column, and the location of this table in the file is given by a row.

For instance:

$startTable 

0 1 4

1 2 4

2 1 4 

$endTable 

Thus, the location of this table is determined by the line number associated with $ startTable. I have a value that is stored in a variable (! Test!), So I need a skip =! and then I start adding the third column until I hit $ endTable.

When i try;

for /f "skip=!test! tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "$endTable" goto :break
set /a test2+=%%j
)
:break

I get the following error:

!test! tokens=3 delims= " was unexpected at this time.

-GK

+3
source share
3 answers

for . , . % wrapped - , .

, :

set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do ( echo %%j

EDIT: , . , , % j .

stackoverflow_input.txt

$startTable

0 1 3

1 2 4

2 1 5

$endTable

stackoverflow1.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

stackoverflow2.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
for /f "skip=%test% tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

( Win 7, Server 2008R2, Server 2003 Win XP SP3):

D:\temp>stackoverflow1.bat
4
5
Sum: 9

D:\temp>stackoverflow2.bat
4
5
Sum: 9

, : /f was unexpected at this time.

+3

.

set num=2
for /F "skip=%num% tokens=3 delims= " %%i in (myfile.txt) do echo %%i

. !

+1

. test.txt

set sk=3  
for /f "skip=%sk% delims=" %%L in (test.txt) do (  
    echo %%L  
    )  
+1

All Articles