Solved

A while - do loop in DOS?

Posted on 2004-03-23
13
12,026 Views
Last Modified: 2007-12-19
I have a little bat and txt file that I would like to use to help me run a script against 3 different programs. The idea as Im trying to make the script as generic and less text intensive. Instead of having a stanza for each program I can use this little deal I worked out to assist me in the endeavor.

Here are the 2 files (there shouldnt have to be any editing to these, there just here for background)
-----------------------
prog.bat
prog.txt

prog.bat
------------------------------------
@echo off

set /P PROGRAM=PROGRAM NAME [%PROGRAM%]:

set PHOME=
for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do if /I "%%a"=="%PROGRAM%" set PHOME=%%b

if "%PHOME%"=="" set /P PHOME=PROGRAM HOME:

echo Program Number: %PROGRAM%
echo Program Home  : %PHOME%


prog.txt
----------------------------
program1:C:\program1
program2:E:\program2
program3:F:\program3


What this does is allows the user to run prog.bat from the command line and enter the name like program1 and it will set the path to the home automatically (C:\program1) then they can use the program.

So what I want to do is setup a script that will use this to run the same script against three programs....

Heres the logic as I see it....

It will go through prog.txt 1 by 1 and set the environment and execute the script then come back and set the next and execute the script...etc.


set scriptname =tada.sql
while (there is programs in prog.txt that I need to run )

prog.bat  <------ get entry from prog.txt, and set env.....so first would be (program1, then program2, then program3, done)

sqlplus user/pass@%program%   @%scriptname%

do

I need to use the the prog.bat utility as it sets the home path, the sqlplus string will work just matter of getting the program to loop and pull the input.


0
Comment
Question by:amenoss
  • 6
  • 6
13 Comments
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Not following you... Prog.bat allows user to select a program as defined in your prog.txt file. But, the only thing annotated in prog.txt is the program name and the path. What about the parameters? You make a reference to sqlplus and it's accepting as a parameter %program% and %scriptname%. I see where %program% comes from, but am unsure why sqlplus would care about a program name. Is %scriptname% always equal to tada.sql?

I couldn't see using prog.bat for this processing without modification, since it prompts the user for a program name. I could see using the for statement to parse the prog.txt file.

This script does what you specify, but I'm not sure what use it would be:

@echo off

for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do sqlplus user/pass@%%a tada.sqlset

The for loop acts as a while loop, but you already know that.

Good Luck,
Steve

0
 

Author Comment

by:amenoss
Comment Utility
Steve -

Does is make more sense if I tell you prog = databases, thus the sql runs against each one and reports items...thus we run the same script against all databases.
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
That makes more sense :)

How does my for loop work for your processing? It doesn't use the home, but it could very easily.

for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do sqlplus user/pass@%%b\%%a tada.sqlset

Not sure if that is correct.
0
 

Author Comment

by:amenoss
Comment Utility
Steve, Im not all the familiar with the for loop, and thanks for the help!

You are correct though I think Im almost there with this but I cant get it to work just right, I am trying to set a variable
after the do, I need to have it set so that it is part of the report name:

for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do

set program =%%a
sqlplus user/pass@%%a @tada.sql
type %program%.txt >> final report

 
I think im missing something easy?
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 125 total points
Comment Utility
Try this:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do (
  set program=%%a
  sqlplus user/pass@%%a @tada.sql
  type !program!.txt >> "final report"
)




0
 

Author Comment

by:amenoss
Comment Utility
Were close Steve its having a problem with the type line I think and Im not sure why?

Gives an error processing program1
then an error processing .txt

echo final report > final.txt
for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do (
  set program=%%a
  echo %program% >> final.txt
  sqlplus user/pass@%%a @tada.sql
  type !program!.txt >> final.txt
)

I tryed to echo %program% >> final.txt  (it says echo is off in the final.txt file.)

The header shows up in the final.txt file also.

the sql is running fine and generating the file it supposed to on every run.. which is %program%.txt
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
You have to use this:

SETLOCAL ENABLEDELAYEDEXPANSION
0
 

Author Comment

by:amenoss
Comment Utility
sorry this is the top part of the script..

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
echo final report > final.txt
for /f "tokens=1* delims=:" %%a in (c:\WINNT\system32\prog.txt) do (
  set program=%%a
  echo %program% >> final.txt
  sqlplus user/pass@%%a @tada.sql
  type !program!.txt >> final.txt
)

There any reason why i cant echo out %program% to the file it says echo off instead of the value..
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Can't echo program for the same reason you have to use SETLOCAL ENABLEDELAYEDEXPANSION. Check set /? for more information on that. You have to do:

echo !program! >> final.txt




0
 

Author Comment

by:amenoss
Comment Utility
Ok I see where you going with that now, thanks for that info that clears some things up, but what it looks like is on the line

where were doing type !program!.txt >> final.txt   its looking at !program!.txt as two things looks like it putting a space between them so it thinks theres two things program1 and .txt  instead of program1.txt..

so it gives an error processing program1 and then one right after for .txt


0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Well your prog.txt file doesn't have spaces after program name does it?

Make sure that this like doesn't have an extra space at the end:

set program=%%a

0
 
LVL 8

Expert Comment

by:K_2K
Comment Utility
Forgive me butting in, this should help get rid of the space if there's one beween the names and the | in prog.txt file as it appears to be:

.
.
.
set program=%%a
if "!program!"=="!program:~,-1! " set program=!program:~,-1!
.
.
.

Also,  eliminating white space from delims= leads me to believe you expect spaces in some of the database names, or at least wish to allow it.  If that's so all references to it as a file name argument should be quoted, as in:
    type "!program!.txt">> final.txt


<< There any reason why i cant echo out %program% to the file it says echo off instead of the value.. >>

If you mean from your command window after running the wcript, then yes, that's what SETLOCAL was designed to do - NOT fill up your environment space with new variables NOR change the ones you have.  it makes a loval set of variables that dissappears when the script finishes.


This is all still SteveGTR's work, no credit for me please.


HTH
2K
(\o/)
0
 

Author Comment

by:amenoss
Comment Utility
Sorry for the delay in my response, Steve you were right on the line

set program=%%a  there was a space behind the the a

works great now....K2K thanks for your input as well.


Thanks Steve!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now