Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29383 Discussions

creating a batch file within FORTRAN

bhvj
Beginner
2,954 Views

I have a set of output files that are created by a FORTRAN program in a directory, with which I would like to perform a set of operations such as copy, rename, and execute another FORTRAN executable using these output files as the arguments, all in a batch mode. Is there a way in FORTRAN to perform all this operations together in one FORTRAN program? Thank you in advance for any help/suggestions in this regard.

 

0 Kudos
8 Replies
Steven_L_Intel1
Employee
2,954 Views

Yes, this can be done, but requires use of OS-provided library routines in some cases. (By the way, the language name is Fortran, not FORTRAN - it has been spelled "Fortran" since 1991.)

You don't have to use batch files or scripts - each of the operations can be done from a program. The Fortran standard intrinsic procedure EXECUTE_COMMAND_LINE can execute arbitrary commands you name in a character variable.

Renaming files can be done with the Intel extension library routine RENAMEFILEQQ.

There isn't a library routine to copy files. You could do this with EXECUTE_COMMAND_LINE or could do the file copying record-by-record in ordinary Fortran code

You could also use Fortran code to create a shell script and then use EXECUTE_COMMAND_LINE to invoke it. I will comment that EXECUTE_COMMAND_LINE requires compiler version 15.0.1 or later.

0 Kudos
bhvj
Beginner
2,954 Views

Steve,

           Thank you for your response and suggestions. I will correct the language from now on. I believe I have intel compiler version 11.1. So, I cannot use the Fortran standard intrinsic procedure EXECUTE_COMMAND_LINE, right? Is this the same with RENAMEFILEQQ ? If I can use RENAMEFILEQQ with intel compiler version 11.1, can you please let me know an example statement for executing this in a batch mode?

You also mentioned that I could do the file copying record-by-record in ordinary Fortran code, can you please elaborate, or let me know an example for this.

Thank you very much for your help.

0 Kudos
Steven_L_Intel1
Employee
2,954 Views

RENAMEFILEQQ is in version 11.1. As for copying a file, you would OPEN the old file and the new one, READ records from the old one and WRITE to the new. It's very simple.

You can use the SYSTEM library routine (add USE IFPORT to make it available, same with RENAMEFILEQQ) as an alternative to EXECUTE_COMMAND_LINE.

0 Kudos
bhvj
Beginner
2,954 Views

Thank you Steve,

                            Got you now regarding RENAMEFILEQQ and the file copying record-by-record in ordinary Fortran code. 

If I want to do the  following operations from a Fortran program (and not by calling a shell script), how do I do that:

1. Changing directory

2. Piping output to a certain directory

3. Executing another Fortran executable

Thank you very much for your response and suggestions.

0 Kudos
Steven_L_Intel1
Employee
2,954 Views

I'm not going to write code for you. You can look up the library routines in the documentation. There is a CHANGEDIRQQ to do a "cd". As for piping output, you would specify that on the command to be executed by SYSTEM.

0 Kudos
bhvj
Beginner
2,953 Views

As regards the usage of SYSTEM function, as given in the documentation is as follows:

   USE IFPORT
   INTEGER(4) I, errnum
   I = SYSTEM("dir > file.lst")
   If (I .eq. -1) then
     errnum = ierrno( )
     print *, 'Error ', errnum
   end if
   END

I understand that this is for an interactive environment. In a batch mode, I did not quite get it, does the statement have to be:

SYSTEM (operating system command)

as an example SYSTEM (cp X Y), in order to execute a Linux command "cp X Y" (copying file X to a file Y) in a Fortran program.

Or am I missing something?

I will try to pick up from any suggestions.

Thank you Steve.

 

0 Kudos
mecej4
Honored Contributor III
2,954 Views

Instead of making unfounded speculations and asking us to comment about them, please consult the documentation. The statement I = SYSTEM (cp X Y) is not valid in free form Fortran, no matter what function name is used in place of SYSTEM. Secondly, the interface to this specific function is "function of return type integer that takes a single character variable of arbitrary length as argument", as you will see when you consult the documentation. Therefore, you must write I = SYSTEM ('cp X Y')

Note that the string argument is interpreted by the command processor. Fortran does not concern itself with what the string contains. Note further that this particular string is not a recognizable command to the Windows command processor unless cp is a user-contributed program, but Unices do have a utility program with this name. That is why such programs (i.e., those that call the OS) are in general not portable.

You seem to have some preconceived notions of what batch mode and interactive mode signify, but you have not stated what those notions are. When a standard Fortran program runs, it is agnostic as to whether it is being run in batch mode or interactive mode, just as individual character mode programs invoked from a shell script are not aware of whether the commands that caused them to run came from a script file or were typed in by the user. Note that Fortran and operating systems existed since the 1950s but GUIs did not become widely used until the 1980s. 

0 Kudos
bhvj
Beginner
2,954 Views

Sorry about that folks, I found out that I did have a wrong notion about SYSTEM function example as being used for interactive mode. I was actually looking at several portability functions at the same time. I had this impression from when I was looking at the RENAMEFILEQQ example:

 USE IFPORT
USE IFCORE
INTEGER(4) len
CHARACTER(80) oldname, newname
LOGICAL(4) result

WRITE(*,'(A, \)') ' Enter old name: '
len = GETSTRQQ(oldname)
WRITE(*,'(A, \)') ' Enter new name: '
len = GETSTRQQ(newname)
result = RENAMEFILEQQ(oldname, newname)
END
 

In this example I thought the only way this program would be getting the values for "oldname" and "newname" is interactively, but after reading the most recent comment, I understood that this could be just an example, and this could be written for batch mode as well.

Now I have a better understanding of these portability functions. As always, thank you very much for all your valuable responses.

0 Kudos
Reply