Computing.Net > Forums > Programming > NT Batch: Variables in FOR DO loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

NT Batch: Variables in FOR DO loop

Reply to Message Icon

Name: deivakumar
Date: February 15, 2009 at 01:40:57 Pacific
OS: Windows XP
CPU/Ram: (N/A)
Product: (n/a) / (n/a)
Subcategory: General
Comment:

I have the a batch file called "Test.bat":

---------------Test.bat----------------
SETLOCAL EnableDelayedExpansion

SET LIST=Apple Orange

SET NAME=XXXXX

FOR %%G IN (%LIST%) DO (
SET NAME=%%G
START /WAIT CMD /C "printname !NAME!"
ECHO !NAME!
SET NAME=!NAME! is a fruit
START /WAIT CMD /C "printname !NAME!"
ECHO !NAME!
)

ENDLOCAL
-----------End of Test.bat-------------

I also have another batch file called "printname.bat" which is called by test.bat:
-------------printname.bat-------------
ECHO %1
PAUSE
----------End of printname.bat---------

When I execute test.bat, this is the output I see on the main window:
----------------OUTPUT----------------
Apple
Apple is a fruit
Orange
Orange is a fruit
------------End of OUTPUT-------------

This is, however, not the problem.

Each loop has two calls to printname.bat, where each call to printname,bat is spawned in separate windows. The variable %NAME% is passed in to printname.bat. In each of the loops, both the calls to printname.bat show only "Apple" or "Orange". When the variable is changed to "Apple is a fruit" or "Orange is a fruit", I do not see the changed string being displayed in the separate command windows which are launched by the lines "start /WAIT cmd /C ...".

What do I need to do to be able to get the separate command windows to show the updated variable?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: February 15, 2009 at 01:48:35 Pacific
Reply:

START /WAIT CMD /C "printname !NAME!"
This is the most awesomest way to do:

call printname %%G

I do not see the changed string being displayed in the separate command windows
%1 = Apple
%2 = is
%3 = a
%4 = fruit
%* = Apple is a fruit

Or add quotes in the main script:
%1 = "Apple is a fruit"


0

Response Number 2
Name: deivakumar
Date: February 15, 2009 at 02:13:49 Pacific
Reply:

The reason I use "START /WAIT CMD /C ..." is because I want that part to be spawned in a separate shell window. It is done intentionally.

However, I cannot see the updated !NAME! variable being displayed in the second call to printname.bat in the loop.

Any ideas how I can get the spawned windows to display the updated variable contents?


0

Response Number 3
Name: Razor2.3
Date: February 15, 2009 at 02:33:23 Pacific
Reply:

That's pretty much what the other 2/3 of my post was about.

EDIT: Or, if you need it spelled out, change your second script to read:

ECHO %*
PAUSE


0

Response Number 4
Name: Judago
Date: February 15, 2009 at 13:24:03 Pacific
Reply:

Why not do away with printname.bat and just use:

START /WAIT CMD /C "echo !NAME!&&pause"


0

Response Number 5
Name: deivakumar
Date: February 15, 2009 at 17:26:22 Pacific
Reply:

Razor2.3 -
EDIT: Or, if you need it spelled out, change your second script to read

As you said, your first post spelt out the solution, but my eyes did not catch it. Thanks for pointing it out to me. Not sure how I missed that. :P


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

C++ error:How to solve th... c language



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: NT Batch: Variables in FOR DO loop

ASP - using a variable in a Do Loop www.computing.net/answers/programming/asp-using-a-variable-in-a-do-loop/6352.html

Variables in For loops, Batch www.computing.net/answers/programming/variables-in-for-loops-batch/17037.html

Adding characters to variable in FOR loop www.computing.net/answers/programming/adding-characters-to-variable-in-for-loop/19704.html