?
Solved

A while - do loop in DOS?

Posted on 2004-03-23
13
Medium Priority
?
12,080 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10660075
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
ID: 10661221
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
ID: 10661617
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:amenoss
ID: 10661636
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 500 total points
ID: 10661778
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
ID: 10662805
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10662821
You have to use this:

SETLOCAL ENABLEDELAYEDEXPANSION
0
 

Author Comment

by:amenoss
ID: 10662859
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
ID: 10662874
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
ID: 10663033
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
ID: 10663333
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
ID: 10667264
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
ID: 10687023
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

800 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