Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12107
  • Last Modified:

A while - do loop in DOS?

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
amenoss
Asked:
amenoss
  • 6
  • 6
1 Solution
 
SteveGTRCommented:
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
 
amenossAuthor Commented:
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
 
SteveGTRCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
amenossAuthor Commented:
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
 
SteveGTRCommented:
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
 
amenossAuthor Commented:
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
 
SteveGTRCommented:
You have to use this:

SETLOCAL ENABLEDELAYEDEXPANSION
0
 
amenossAuthor Commented:
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
 
SteveGTRCommented:
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
 
amenossAuthor Commented:
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
 
SteveGTRCommented:
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
 
K_2KCommented:
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
 
amenossAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now