1

I am having a problem figuring out how to populate a file with variables and then running a loop to print series of lines.

Here are the codes:

Batch 1:

@echo off

:: This batch read a file and copy all lines containing that word into a new 
file in an ordered list. (This works just fine)

findstr /C:"wordA" OLD.txt >> list_of_variables.txt
for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%

The result is something like this

wordA 1111 wordb
wordA 1112 wordb
wordA 1113 wordb
wordA 555 wordb

Batch 2:

@echo off
cls

:: This batch is supposed to get the variable %string% and look in a different file (old.txt) and copy a block of 10 lines below the matching string.

setlocal enabledelayedexpansion
set string=%string%
for /f "tokens=*" %%1 in (OLD.txt) do ( 
        if !flag! equ 1 (
         echo !string! %%1 >> output.txt
         set /a count+=1
         if !count! gtr 10 goto endit
         )
    if /i "%%1" equ "!string!" (set flag=1)
)
echo "%string%" not found check spellings and input file.
exit /b

:endit
type output.txt

The intended result will be something like:

|-same as string|  | read form old.txt|
wordA 1111 wordb   wordc word worde worf
wordA 1111 wordb   wordg worh wordi worj

Here is the deal:

If I use them separately they both work fine, but when I try to make them work together it does not work. The batch 2 with worda set as set string=worda works like a charm, so I know it is correct but when I pass the variable from the batch 1, it does not print anything in the output.txt file.

Other solution is to call the 2 loops in the same batch file but I haven't been able to figure it out.

Any assistance or guidance will be highly appreciated.

Jonathan.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    another [delayed expansion](http://stackoverflow.com/a/30284028/2152082) problem. – Stephan Sep 22 '16 at 17:45
  • Why are you calling the 2nd batch file with an argument and then not using that argument in your 2nd batch file? – Squashman Sep 22 '16 at 18:11
  • Squashman, that is because I have made many changes to make it work and I may have left out a lot of things. That is why I pasted both bat files to get ideas on how to fix this. – Jonathan Monestel Sep 22 '16 at 18:59

2 Answers2

0

in your first batchfile, the line:

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%

gets parsed as a whole before executing. %string% is still empty at that time. You have to use delayed expansion:

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat !string! 
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you Stephan, I made the change however still does not work. The batch 2 for loop does not kick in for some reason. – Jonathan Monestel Sep 22 '16 at 18:06
  • bat1 gives a parameter to bat2. You can reference it in bat2 with `%1` (= first parameter). Btw. don't use numbers as `for` variables (`%%1`). Although it works, it's confusing, as `%1` means "the first parameter". – Stephan Sep 22 '16 at 18:10
  • Thank you Stephan, I made the changes and used letters instead. – Jonathan Monestel Sep 22 '16 at 19:00
0

In batch1, change

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%

to

for /f "delims=" %%x in (list_of_variables.txt) do call dp2.bat %%x

Strangely, you are using delayedexpansion in the second batch, but not in the first. delayedexpansion allows access to the run-time value of a variable within a loop where the variable's value changes. %var% means "the original value of var when the line was encountered" and !var! means "the value of var as it changes through the operation of the commands"

In the second batch, change each %%1 to %%q (or %%any letter, being consistent with case.) %1 means the value of the first parameter provided to the routine and attempting to use it as a metavariable is prone to disaster and considered bad practice in batchworld.

You can then set the value of string to the value of the first parameter provided (by batch1) using

set "string=%1"

Note that if the string being passed contains spaces or other separators, then you should quote the parameter being passed "%%x" and dequote the string on assignment set "string=%~1" (note the ~)

BUT, with you code as it stands, string would be set up in the environment by the first batch and the second batch would see string as it was set by batch1; the set string=%string% is redundant.

Your problem is, I believe, attempting to use %%1 as a metavariable.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Might have to put quotes around the FOR variable. `call dp2.bat "%%x"`. – Squashman Sep 22 '16 at 18:41
  • Thank you Magoo, with your changes now the 2nd batch copies the first 10 lines of the file over and over, not searching for the string and copy 10 lines down. – Jonathan Monestel Sep 22 '16 at 19:01
  • you should initialise the variable `flag` before the `for` loop. If it's set to 1 in the environment before batch2 runs, it willl have the value `1` to start with and hence will spit out the first 10 lines. Would be an idea to initialise set the variable `count` too. If it's undefined, the `set /a` will assume a value of zero. Personally, for an on/off action like `flag` is used, I set it to *nothing* (`set "flag="`) or *something* (anything except *nothing*.) You can then use `if defined flag ...` or `if not defined flag...` to interpret its state. – Magoo Sep 22 '16 at 19:31