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

call cmd.exe in fortran program and write and execute a command on cmd.exe

john1981
Beginner
2,477 Views
hi

I want to call cmd.exe ( c:\\windows\\system32\\cmd.exe)from my fortran programto write a command on it and execute this command by cmd.exe
How can I do this? I want to do this by systemqq,but I dont know what to do?
thanks in advance.
0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
2,477 Views
John,

Open(Unit=966,File='C:\Program Files\MATLAB\R2010b\bin\1.bat'
* ,Action='Write',Status='old',position='asis')
write(966,*)'fismain fis_in hf.fis > fis_out'
close(966)

The above creates the batch file in the folder 'C:\Program Files\MATLAB\R2010b\bin'
Which is probably not a good place to place the file.
None the less, assuming that is where you want the batch file, then your

RESULT=SYSTEMQQ('1.bat')

is attempting to run the 1.bat file from the current directory

To run the file where you located it you would need

RESULT=SYSTEMQQ(''C:\Program Files\MATLAB\R2010b\bin\1.bat')

*** note
your fismain, fis_in hf.fis and fis_out will be assumed to reside in your current directory (wherever that is)

You are not keeping strait:

a) where your program resides
b) what the current directory is when you run your program
c) where MATLAB is
d) where you create and use your fismain
e) where you create and use fis_in
f) where you create and use hf.fis
g) where you create and use fis_out

When you omit the paths (perfectly reasonable) then all files default to "the current directory" wherever that is, with the exception to the execuitable file(s) may also reside in PATH.

Typicaly it is bad practice to write your files into the system install path for applications (C:\Program Files\MATLAB\...) instead you should write them to a directory you create for your purposes such as:

C:\MyMATLAB or C:\TEMP

Or to "current directory" where the current directory is setup before hand to point to one of your directories setup for the purpose of running your programs.

Which of: fismain fis_in hf.fis and fis_out are your files?

If these are all your files (where fismain is an execuitable youcreate that uses MATLAB.DLL etc, and the DLL is registered or in path) then you wouldwant something like

C:\TEMP\fismain\x64\Release\fismain.exe
C:\TEMP\fismain\fis_in
C:\TEMP\fismain\hf.fis
C:\TEMP\fismain\fis_out

When you run from VS IDE theyourlittle program that creates and runs the batch file you likely have

C:\TEMP\YourLittleProgram\x64\Release\YourLittleProgram.exe

And when you run it, the current directory default to

C:\TEMP\YourLittleProgram

Therefore inside YourLittleProgram.exe you would likely use something along the line of

Open(Unit=966,File='1.bat', ...
write(966,*) 'CD C:\TEMP\fismain'
write(966,*)'x64\Release\fismain fis_in hf.fis > fis_out'
close(966)
RESULT=SYSTEMQQ('1.bat')
! *** note, depending on system settings the current directory
! *** may remain C:\TEMP\fismain or get restored.

Jim Dempsey

View solution in original post

0 Kudos
19 Replies
jimdempseyatthecove
Honored Contributor III
2,477 Views
When the command is a single line, just place the command inside the quotes for systemqq


IF(.NOT. SYSTEMQQ('NOTEPAD FOO.TXT') STOP 'NOTEPAD NOT FOUND'

When the command is multi-linecreate and usea batch file

OPEN(10, STATUS='REPLACE', FILE='YourFileNameHere.BAT')
WRITE(10,*) 'DIR > FOO.TXT'
WRITE(10,*) 'NOTEPAD FOO.TXT'
CLOSE(10)
IF(.NOT. SYSTEMQQ(''YourFileNameHere.BAT') STOP 'ERROR'

Jim Dempsey
0 Kudos
john1981
Beginner
2,477 Views

Dear Jim
Hi
Thanks for your reply.I want to open cmd.exe,write sth like
fismain fis_in hf.fis > fis_out on cmd.exe and execute it.
How canI do that by systemqq in intel fortran program?
thanks.
john

0 Kudos
DavidWhite
Valued Contributor II
2,477 Views
You should be able to put the whole command within prompts like jim suggests. Only thing you need to make sure is that flsmain is accessible on the path, otherwise will need to include the full path within the string.

Regards,

David
0 Kudos
jimdempseyatthecove
Honored Contributor III
2,477 Views
IF(.NOT. SYSTEMQQ('fismain fis_in hf.fis > fis_out')) GOTO 9999 ! 9999on error

Note, as David stated, the current drive and directory have to be appropriate as the above syntax assumes all resides in the current directory on the current drive.

When running in Debug mode, and if you do not do anything otherwise, the default drive and directory are those of the Project. Therefore, if you have

Solution YourSolutionNameHere
Project fismain
Project FooBar
...

And if you do not set the "active directory" property in the FooBar Project to point to fismain Project directory.
Or if the path to fismain is not in PATH.
Then you wil need something like

IF(.NOT. SYSTEMQQ('..\fismain\fismain fis_in hf.fis > fis_out')) GOTO 9999 ! 9999on error

Jim Dempsey
0 Kudos
john1981
Beginner
2,477 Views
dear jim
Thanks alot.I tried both of your suggestions, But my program in both cases didn't compiled.
So I used this statement CALL SYSTEMQQ('..\fismain\fismain fis_in hf.fis > fis_out')
and CALL SYSTEMQQ('fismain fis_in hf.fis > fis_out'). In both cases, my program compiled.
Let me explain my goal:
fismain is afuzzy engine in matlab,
fis_in is an input file,
hf.fis is a fuzzy program
fis_out is a output file.
Iexecutethis statement fismain fis_in hf.fis > fis_out
on cmd.exe to calculate fis_out by fismain and It works.like this:

Microsoft Windows [Version 6.1.7600]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Program Files\MATLAB\R2010b\bin>fismain fis_in hf.fis > fis_out

Butwhen Iexecute this statement by my program(call syetemqq)
It didnt work and fis_out is empty.
please explain what to do
thanks in advance
john

0 Kudos
john1981
Beginner
2,477 Views
Dear David
Hi
Thanks for your reply.
Regards,
John
0 Kudos
Les_Neilson
Valued Contributor II
2,477 Views
John
You say you tried running justthe command itself from a cmd window to see if it works. It would need to be from the same directory where you run your program so that the relative path you specify is applicable.

Note :SYSTEMQQ is a (logical) function and NOT a subroutine so should be invoked as
result = SYSTEMQQ(commandline)
not as CALL SYSTEMQQ

Also, from the help (if result is .FALSE.)

...
If the function fails, call GETLASTERRORQQ to determine the reason. One of the following errors can be returned:

ERR$2BIG - The argument list exceeds 128 bytes, or the space required for the environment formation exceeds 32K.

ERR$NOINT - The command interpreter cannot be found.

ERR$NOEXEC - The command interpreter file has an invalid format and is not executable.

ERR$NOMEM - Not enough memory is available to execute the command; or the available memory has been corrupted; or an invalid block exists, indicating that the process making the call was not allocated properly.
...

You say earlier attempts did not compile - what compile error message(s) did you get?

Les

0 Kudos
jimdempseyatthecove
Honored Contributor III
2,477 Views
John,

Apparently SYSTEMQQ is not handling the '>' properly (at least on my system).
To work around this....

create fis_in_out.bat

containing:

fismain $1$2 > $3

Note, if you use NOTEPAD, make sure you use Save As with *.* (all files) for type
(else you end up with fis_in_out.bat.txt which will not work)

This will require you to specifythree arguments to the batch file

Then in your program use

IF(.not. SYSTEMQQ('fis_in_out.bat fis_in hf.fis fis_out')) goto 9999

Jim Dempsey
0 Kudos
john1981
Beginner
2,477 Views

Dear Les
hi
My program,fismain, fis_in,fis_out,hf.fis and cmd.exe are in the same folder.
My program is an abaqus subroutine written by intel fortran 11.
I want to execute cmd.exe by this subroutine, and read fis_out after execution of cmd.exe
I don't know about systemqq. when I used this
IF(.NOT. SYSTEMQQ('fismain fis_in hf.fis > fis_out')) GOTO 9999 ! 9999 on error
my abaqus subroutine didnt compile.
I dont know about systemqq and bat file please explainhow toexecute this commandby cmd.exe from my program the command is fismain fis_in hf.fis > fis_out
please explain it by details.
thanks in advance.
Regards,
John

0 Kudos
john1981
Beginner
2,477 Views
Dear Jim
hi
I dont know how to thank you. Thanks a lot for your help.
Unfortunately I dont know about bat file, how to create fis_in_out.bat, what is

containing:

fismain $1 $2 > $3

Actually My program is a subroutine Program written in intel fortran11.
I want to execute a command on cmd.exe from my program ( use fuzzy matlab engine )

please explain how to execute my command by batch file.

please explain it by details.

If u have any documents about batch file and systemqq that can help me please send me.
thanks in advance.
Regards,
John

0 Kudos
john1981
Beginner
2,477 Views

Dear Jim
hi
I made a bat file and on this bat file I write sth like fismain fis_in hf.fis > fis_out .
After executing this file, It works. But when I make another bat file with this content fismain $1 $2 > $3
and I used IF(.not. SYSTEMQQ('fis_in_out.bat fis_in hf.fis fis_out')) goto 9999 , my program didnt compile. My Program is an abaqus subroutine written by intel fortran 11. Could you please explain why it dont compile ? should I add 9999 lable for the last line of my program?
thanks in advance
Regards,
John

0 Kudos
jimdempseyatthecove
Honored Contributor III
2,477 Views
>>>and I used

IF(.not. SYSTEMQQ('fis_in_out.bat fis_in hf.fis fis_out')) goto 9999


John,

When you receive suggestions on this forum, the suggestions make an assumption that the reader will fill in the blanks (complete the program).

SYSTEMQQ is a LOGICAL FUNCTION

This means it returns a boolean value for success or fail.

LOGICAL :: YourLogicalErrorVariable
... (some of your code here) ...
! one way of haneling thefunction
YourLogicalErrorVariable = SYSTEMQQ('some command line here')
IF(.NOT. YourLogicalErrorVariable)THEN
WRITE(*,*) "Error executing SYSTEMQQ"
STOP
ENDIF
... (continue running here if no error) ...
...
! alternate wayof handling the function
IF(.NOT.SYSTEMQQ('some command line here')) GOTO 9999
... (continue running here if no error) ...
RETURN
! Your error handling below
9999 WRITE(*,*) "Error executing SYSTEMQQ"
STOP
! other error codes follow
The above illustrates both techniques.

you must fill in the "..." with your code.

When a library function returns a value, such as an indicator of success or failure, you must test return and take appropriate action (else suffer the consequences of run time errors).

Jim Dempsey
0 Kudos
DavidWhite
Valued Contributor II
2,477 Views
Something like
OPEN (99,file='fis_in_out.bat')
WRITE(99,'(A)') 'fismain $1 $2 > $3'
CLOSE(99)
then call SYSTEMQQ with command 'fis_in_out.bat' instead of the original command.

David
0 Kudos
jimdempseyatthecove
Honored Contributor III
2,477 Views
>>then call SYSTEMQQ with command 'fis_in_out.bat' instead of the original command

David,

I think you mean

SYSTEMQQ ('fis_in_out.bat fis_in hf.fisfis_out')

John,

The purpose of this round about way is to eliminate the ">" from the string argument to SYSTEMQQ

The $1 $2 and $3 in the batch file take the batch file's command line arguments number 1, 2, and 3 respectively and inserts the text into a new command line, thus producing

fismain fis_in hf.fis> fis_out

Jim Dempsey
0 Kudos
DavidWhite
Valued Contributor II
2,477 Views
Jim,

thanks for correcting my post - i can't remember the last time I wrote a serious batch file with arguments! it was once "bread and butter", how technology marches on.

David
0 Kudos
john1981
Beginner
2,477 Views

Dear Jim
Thanks for your support. I have tried your suggestion( In two cases )
1:
C My program is located in C:\Temp

Cbut fismain,fis_in,hf.fis,fis_out are located in C:\Program Files\MATLAB\R2010b\bin

Open(Unit=966,File='C:\Program Files\MATLAB\R2010b\bin\1.bat'

* ,Action='Write',Status='old',position='asis')

write(966,*)'fismain fis_in hf.fis > fis_out'

close(966)

RESULT=SYSTEMQQ('1.bat')

IF(.NOT. RESULT) THEN

WRITE(*,*) 'Error executing SYSTEMQQ'

END IF


2:

C My program is located in C:\Program Files\MATLAB\R2010b\bin
C but fismain,fis_in,hf.fis,fis_out are located in C:\Program Files\MATLAB\R2010b\bin


Open(Unit=966,File='C:\Program Files\MATLAB\R2010b\bin\1.bat'

* ,Action='Write',Status='old',position='asis')

write(966,*)'fismain $1 $2 > $3'

close(966)

RESULT=SYSTEMQQ('1.bat fis_in hf.fis fis_out')

IF(.NOT. RESULT) THEN
WRITE(*,*) 'Error executing SYSTEMQQ'
END IF

In both cases when I open output file ( I mean fis_out or $3 )
I recieved these messages :

The file fis_in cannot be opened.

The file $1 cannot be opened.


I'm working to solve this problem, If you have any suggestion, please tell me.

Thanks in advance.

Regards,

John

Path: p


  • Mark this post Private ?

    YESNO
  • Quote this post ?

    YESNO

  • 0 Kudos
    jimdempseyatthecove
    Honored Contributor III
    2,478 Views
    John,

    Open(Unit=966,File='C:\Program Files\MATLAB\R2010b\bin\1.bat'
    * ,Action='Write',Status='old',position='asis')
    write(966,*)'fismain fis_in hf.fis > fis_out'
    close(966)

    The above creates the batch file in the folder 'C:\Program Files\MATLAB\R2010b\bin'
    Which is probably not a good place to place the file.
    None the less, assuming that is where you want the batch file, then your

    RESULT=SYSTEMQQ('1.bat')

    is attempting to run the 1.bat file from the current directory

    To run the file where you located it you would need

    RESULT=SYSTEMQQ(''C:\Program Files\MATLAB\R2010b\bin\1.bat')

    *** note
    your fismain, fis_in hf.fis and fis_out will be assumed to reside in your current directory (wherever that is)

    You are not keeping strait:

    a) where your program resides
    b) what the current directory is when you run your program
    c) where MATLAB is
    d) where you create and use your fismain
    e) where you create and use fis_in
    f) where you create and use hf.fis
    g) where you create and use fis_out

    When you omit the paths (perfectly reasonable) then all files default to "the current directory" wherever that is, with the exception to the execuitable file(s) may also reside in PATH.

    Typicaly it is bad practice to write your files into the system install path for applications (C:\Program Files\MATLAB\...) instead you should write them to a directory you create for your purposes such as:

    C:\MyMATLAB or C:\TEMP

    Or to "current directory" where the current directory is setup before hand to point to one of your directories setup for the purpose of running your programs.

    Which of: fismain fis_in hf.fis and fis_out are your files?

    If these are all your files (where fismain is an execuitable youcreate that uses MATLAB.DLL etc, and the DLL is registered or in path) then you wouldwant something like

    C:\TEMP\fismain\x64\Release\fismain.exe
    C:\TEMP\fismain\fis_in
    C:\TEMP\fismain\hf.fis
    C:\TEMP\fismain\fis_out

    When you run from VS IDE theyourlittle program that creates and runs the batch file you likely have

    C:\TEMP\YourLittleProgram\x64\Release\YourLittleProgram.exe

    And when you run it, the current directory default to

    C:\TEMP\YourLittleProgram

    Therefore inside YourLittleProgram.exe you would likely use something along the line of

    Open(Unit=966,File='1.bat', ...
    write(966,*) 'CD C:\TEMP\fismain'
    write(966,*)'x64\Release\fismain fis_in hf.fis > fis_out'
    close(966)
    RESULT=SYSTEMQQ('1.bat')
    ! *** note, depending on system settings the current directory
    ! *** may remain C:\TEMP\fismain or get restored.

    Jim Dempsey

    0 Kudos
    john1981
    Beginner
    2,477 Views

    Dear Jim

    Thanks a lot. My program works just because of your help.

    First I change my matlab active directory to C:\TEMP\fismain

    Then, put fis_in , fis_out and hf.fis on it.

    Next, put fismain.exe on this path C:\TEMP\fismain\x64\Release

    After that I write these statement on my program:

    Open(Unit=966,File='C:\Temp\1.bat'

    * ,Action='Write',Status='old',position='asis')

    write(966,*) 'CD C:\TEMP\fismain'

    write(966,*)'x64\Release\fismain fis_in hf.fis > fis_out'

    close(966)

    RESULT=SYSTEMQQ('C:\Temp\1.bat')

    IF(.NOT. RESULT) THEN

    WRITE(*,*) 'Error executing SYSTEMQQ'

    END IF

    Also I change my program and It works again ( I mean fis_out has output)

    First I change my matlab active directory to C:\TEMP

    Then, put fis_in , fis_out , hf.fis and fismain.exe on it.

    After that I write these statement on my program:

    Open(Unit=966,File='C:\Temp\1.bat'

    * ,Action='Write',Status='old',position='asis')

    write(966,*) 'CD C:\TEMP'

    write(966,*)'fismain fis_in hf.fis > fis_out'

    close(966)

    RESULT=SYSTEMQQ('C:\Temp\1.bat')

    IF(.NOT. RESULT) THEN

    WRITE(*,*) 'Error executing SYSTEMQQ'

    END IF

    I noticed that my problem was the following:

    1) Changing my matlab active directory to C:\TEMP\fismain

    or C:\TEMP

    2) Adding 'CD C:\TEMP\fismain' or

    'CD C:\TEMP' to my bat file

    Thanks for your reply.

    I wish the best for you.

    Regards,

    John

    0 Kudos
    john1981
    Beginner
    2,477 Views
    Dear David
    Thanks for your support,I could solve my problem just because of you and Jim.
    I wish the best for you.
    Regards,
    John.
    0 Kudos
    Reply