Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Execute a batch file and wait for it to complete

37 views
Skip to first unread message

atmkoh

unread,
Mar 12, 2009, 8:50:23 PM3/12/09
to
I apologise if this topic has cropped up before. I am using
Silverfrost Fortran 95. I am trying to do a shell and wait but without
the hassle of win32 api etc. Is this possible ?

I have to dos batch files that execute proprietary programs.
(Temp1.bat and Temp2.bat)

The idea of putting "look at the first result" and "look at the second
result" is to see if the program will wait for it to finish before
printing and yes it does.

The problem I have is that the first system command spawns a new ms
dos console window which will not close when the program is compelted!
and until I manually close the console.

Is there a library that I can use to actually call the win32 api
direcltly ?

PROGRAM TEST
CALL SYSTEM('START/W C:\Projects\TEMP1.BAT')
PRINT *,' Look at the first result.'
CALL SYSTEM('START/W C:\Projects\TEMP2.BAT')
PRINT *,' Look at the second result.'
END PROGRAM TEST

e p chandler

unread,
Mar 12, 2009, 10:42:59 PM3/12/09
to

1. I can't speak for the behavior of SYSTEM using Silverfrost. It
might interact differently from some other CALL SYSTEM() or i=SYSTEM()
or SYSTEMQQ... or RUNQQ... None of these are standard Fortran.

2. START/W appears to be doing what it is documented to do.

Here is part of the help screen generated on Vista (32) when invoked
by START /?

Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /
BELOWNORMAL]
[/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
[parameters]

.........
WAIT Start application and wait for it to terminate
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to
cmd.exe.
This means that the window will remain after the
command
has been run.

If it is not an internal cmd command or batch file
then
it is a program and will run as either a windowed
application
or a console application.
........

So if you are invoking your program via a batch file with START /W,
which is the same as /WAIT,
the window will stay open, as described above.

3. There are a number of other ways to invoke your program.

Try something like

CALL SYSTEM('C:\Projects\TEMP1.BAT')
PRINT *,'first'
CALL SYSTEM('C:\Projects\TEMP2.BAT')
PRINT *,'second'
END

It depends on what TEMP1.BAT and TEMP2.BAT do, but on my system, when
compiled with g95, the following program

i=system('temp1.bat')
print *,'first done'
read *
i=system('temp2.bat')
print *,'second done'
read *
end

sends all console output to a common screen which closes after the
program ends.

temp1 invokes q1, temp2 invokes q2, q1 prints '1', q2 prints '2'.

If I remember correctly, START was not present in Win95 or Win98, but
was introduced with 2000 or XP.
So it is much more common to either put the name of the .bat file or
the name of the .exe file in the string passed to system rather than
using START. The main reason to use START is to do something unusual.

You used START, and it bit you.

--- e

Larry Gates

unread,
Mar 13, 2009, 10:42:47 PM3/13/09
to
On Thu, 12 Mar 2009 19:42:59 -0700 (PDT), e p chandler wrote:

> You used START, and it bit you.

Elliot's right here. Just for kicks and giggles, I timed the system call
with paul van delst's timer program:


MODULE Timing_Utility
IMPLICIT NONE
PRIVATE
PUBLIC :: Begin_Timing
PUBLIC :: End_Timing
PUBLIC :: Display_Timing
TYPE, PUBLIC :: Timing_type
PRIVATE
INTEGER :: Hertz
INTEGER :: Begin_Clock
INTEGER :: End_Clock
END TYPE Timing_type

CONTAINS

! Subroutine to set the begin time count
! in the timing structure variable
!
SUBROUTINE Begin_Timing( Timing ) ! In/Output
TYPE(Timing_type), INTENT(IN OUT) :: Timing
CALL SYSTEM_CLOCK( COUNT_RATE=Timing%Hertz, &
COUNT =Timing%Begin_Clock )
END SUBROUTINE Begin_Timing

! Subroutine to set the end time count
! in the timing structure variable
!
SUBROUTINE End_Timing( Timing ) ! In/Output
TYPE(Timing_type), INTENT(IN OUT) :: Timing
CALL SYSTEM_CLOCK( COUNT=Timing%End_Clock )
END SUBROUTINE End_Timing

! Subroutine to display the elapsed time between
! the begin and end time counts in the timing
! structure variable
!
SUBROUTINE Display_Timing( Timing ) ! Input
! Arguments
TYPE(Timing_type), INTENT(IN) :: Timing
! Local parameters
REAL, PARAMETER :: N_SECONDS_IN_HOUR = 3600.0
REAL, PARAMETER :: N_SECONDS_IN_MINUTE = 60.0
REAL, PARAMETER :: N_MILLISECONDS_IN_SECOND = 1000.0
! Local variables
REAL :: Total_Time
INTEGER :: n_Hours,n_Minutes,n_Seconds,n_milliSeconds

! Compute the total time in seconds
Total_Time = REAL(Timing%End_Clock - Timing%Begin_Clock) / &
REAL(Timing%Hertz)

! Split the total time into hours, minutes, seconds, and

millseconds
n_Hours = INT(Total_Time/N_SECONDS_IN_HOUR)
n_Minutes =

INT(MOD(Total_Time,N_SECONDS_IN_HOUR)/N_SECONDS_IN_MINUTE)
n_Seconds =

INT(MOD(MOD(Total_Time,N_SECONDS_IN_HOUR),N_SECONDS_IN_MINUTE))
n_milliSeconds =

INT((Total_Time-INT(Total_Time))*N_MILLISECONDS_IN_SECOND)

! Construct the character string
WRITE( *,'("Elapsed time-- ",i2.2,":",i2.2,":",i2.2,".",i3.3 )' )

&
n_Hours, n_Minutes, n_Seconds, n_milliSeconds
END SUBROUTINE Display_Timing
END MODULE Timing_Utility

USE Timing_Utility

TYPE(Timing_type) :: Timing

CALL Begin_Timing(Timing)
CALL SYSTEM('out')
CALL End_Timing(Timing)

CALL Display_Timing(Timing)
endprogram

! gfortran pvd3.f90 -o t.exe

E:\gfortran\dan>gfortran pvd3.f90 -o t.exe

E:\gfortran\dan>t
counter is 785209
trials is 1000000
ratio is 0.78520900000000005
pi fourths is 0.78539816339744828
Elapsed time-- 00:00:00.671
Elapsed time-- 00:00:00.735

E:\gfortran\dan>

So out.exe took 671 milliseconds and the system call was 64 milliseconds,
which I think is huge. Windows must have a lot of overhead.
--
larry gates

Appearances to the contrary notwithstanding, I'm not trying to break
Perl 5 constructs just for the heck of it.
-- Larry Wall in <20041206220...@wall.org>

0 new messages