for with call

S

sali

i have strange behaviour inside batch file:

this works as expected:

---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c *.txt`)
do @echo %%i [%%j]
-----

the purpose is to count the number of matches of searched string, and put it
into %%j
since find /c gives result lines like this:
---------- filename.ext: number_of_match
the idea is to tokenise result line with : delimiter, and to take number
after

but this example bellow doesn't work as expected

---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c *.txt`)
do call :loop %%i %%j
-----

problem is that in second example, delims=: doesn't take effect, and for /f
command uses only default [blank] delimiter

is it expectable, am i wrong somewhere?

thnx
 
B

billious

sali said:
i have strange behaviour inside batch file:

this works as expected:

---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c *.txt`)
do @echo %%i [%%j]
-----

the purpose is to count the number of matches of searched string, and put
it
into %%j
since find /c gives result lines like this:
---------- filename.ext: number_of_match
the idea is to tokenise result line with : delimiter, and to take number
after

but this example bellow doesn't work as expected

---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c *.txt`)
do call :loop %%i %%j
-----

problem is that in second example, delims=: doesn't take effect, and for
/f
command uses only default [blank] delimiter

is it expectable, am i wrong somewhere?

thnx

Can't see from what you say what your objective is.

Do you want to call :LOOP with the filename and count?

If so, try (one line)

for %%i in (*.txt) do for /f %%j in ( ' find /i /c "green" ^<"%%i" ' ) do
call :loop "%%i" %%j
 
M

Matthias Tacke

sali said:
---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c *.txt`)
do call :loop %%i %%j
-----

problem is that in second example, delims=: doesn't take effect, and for /f
command uses only default [blank] delimiter

is it expectable, am i wrong somewhere?

I guess you don't recall to use %1 %2 in the sub.

This should work:

@echo off
for /f "tokens=2,3 delims=: " %%A in (
'find /C /I "echo" *.cmd'
) do call :sub %%A %%B
goto :eof
:sub
echo %1 %2
 
S

sali

billious said:
Can't see from what you say what your objective is.

Do you want to call :LOOP with the filename and count?

If so, try (one line)

for %%i in (*.txt) do for /f %%j in ( ' find /i /c "green" ^<"%%i" ' ) do
call :loop "%%i" %%j

yes! that was the problem.

in call :loop %%i %%j
the param %%i contained space
the called sub expected two params %1 and %2
but since %%i contained space, %1 and %2 received 1st and 2nd space
delimited token from %%i, not the %%j

quoting the %%i like "%%i" solved the prob.

this cmd line param counting is little bit tricky.

thnx!
 
S

sali

Matthias Tacke said:
sali said:
---one line, if wrapped ---
for /f "usebackq tokens=1,2 delims=:" %%i in (`find "green" /i /c
*.txt`)
do call :loop %%i %%j
-----

problem is that in second example, delims=: doesn't take effect, and for
/f
command uses only default [blank] delimiter

is it expectable, am i wrong somewhere?

I guess you don't recall to use %1 %2 in the sub.

This should work:

@echo off
for /f "tokens=2,3 delims=: " %%A in (
'find /C /I "echo" *.cmd'
) do call :sub %%A %%B
goto :eof
:sub
echo %1 %2

thnx, as i described in other post, problem was with param
counting-alignment
since param %%a contained space, called sub took both %1 and %2 from %%a
quoting the %%a like "%%a" helped to preserve it as single param, noto to
split it accros space boundara.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top