1

I have written below script:

@echo off
setlocal EnableDelayedExpansion

REM Collect source filenames from C:\Files and load into C:\doc.txt
dir C:\sources\Sourcefiles /b /a-d > C:\sourcefilenames.txt

REM fetch count of source files and store into variable count
For /F  %%I in ('call C:\count.bat ') Do Set count=%%I

REM loop "count" number of times and echo temp.txt value
FOR /L %%A IN (1,1,%count%) DO (

REM call line.bat to fetch line 1,line 2 and so on of sourcefilenames.txt    for each loop
call line.bat %%A>C:\temp.txt

set /p var=<C:\temp.txt
echo var:%var%    ----------> returns previous run value
type C:\temp.txt  ----------. returns current value of temp.txt

)

Basically what i am trying to do out of the above script is: I am creating a variable(var) from the content of temp.txt(data in temp.txt will change for each time loop runs) to be used in multiple loops.

But the problem i am facing is : Echo var is:%var% command returning me previous run value not temp.txt current content.whereas command "type C:\temp.txt" returning me temp.txt current content. (Note: if i have called/created variable "var" from some other script it returns me that previous value else it returns Null)

Your help/guidance on above issue is really appreciated. Thanks

Sunaj
  • 11
  • 1
  • 1
    you have a [delayed expansion](http://stackoverflow.com/a/30284028/2152082) problem. Use `echo var:!var!`. – Stephan Jun 25 '16 at 18:42

2 Answers2

0

I suppose the variable remains in memory, without being re-read.Attempts to limit the validity of the variable. setlocal echo something..... endlocal or @echo off & setlocal

Wiffzack
  • 315
  • 3
  • 10
  • Thanks for your reply.. Actually i am beginner to Batch scripting.. So could you please edit the script as required and share it with me. – Sunaj Jun 25 '16 at 17:23
0

When CMD.exe encounters a block of code in parentheses, it reads and parses the entire block before executing. This can cause unintuitive behavior. In this case, your echo var:%var% line is being parsed once at the beginning of the loop and never again.

The easiest fix for this is to change that line to

echo var:!var!

The !var! syntax is parsed every time through the loop. This works because you have enabledelayedexpansion set in your script.

Another workaround to this type of problem is to remove the parentheses and instead call out to a subroutine.

FOR /L %%A IN (1,1,%count%) DO call :loopLineBat %%A
... rest of script
exit /b

:loopLineBat
 >%temp%\temp.txt call line.bat %1

<%temp%\temp.txt set /p var=
echo var:%var%
type %temp%\temp.txt
exit /b

This loop does the same as above, but because it is not in a parenthesized block, all of the lines are parsed and executed in order.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • Thanks Stephan & Ryan.. I tried both of the work around & it worked perfectly as i was looking for.. Thanks buddy.. – Sunaj Jun 25 '16 at 18:57
  • Hi Guys, I have one more query. Can you please clarify. I want to store output of below script into a variable. findstr /R /N "^" C:\Users\Sunaj\Desktop\batch_script\doc.txt | find /C ":" I tried to use ------ For /F %%I in ('findstr /R /N "^" C:\Users\Sunaj\Desktop\batch_script\doc.txt | find /C ":"') Do Set var=%%I ----- But seems like due to double quote this above script was not working. Please suggest – Sunaj Jun 25 '16 at 19:26
  • @Sunaj See https://stackoverflow.com/a/1427511 . If that doesn't help, feel free to [ask another question](http://stackoverflow.com/questions/ask). – Ryan Bemrose Jun 25 '16 at 19:29