Fortran 中如何运行Windows命令

SYSTEM: 可以像是在命令行下一样输入一个windows命令
所需模块: USE IFPORT
语法:result = SYSTEM (string)
string
(Input) Character*(*). Operating system command.
Results
The result type is INTEGER(4). The result is the exit status of the shell command.
If -1, use IERRNO to retrieve the error. Errors can be one of the following:
E2BIG: The argument list is too long.
ENOENT: The command interpreter cannot be found.
ENOEXEC: The command interpreter file has an invalid format and is not executable.
ENOMEM: Not enough system resources are available to execute the command.

Example1:
PROGRAM MAIN
USE IFPORT
INTEGER(4) I, errnum
I = SYSTEM(“dir > file.lst”) !把当前文件夹下的文件名列表输出到file.list
If (I .eq. -1) then
errnum = ierrno( )
print *, 'Error ', errnum
end if
END

Example2:
program main
use IFPORT
implicit none
character(len=100) ::filename
character*50 :: command=“cmd”
INTEGER :: FEXIST, RES, STATUS
INTEGER(4) errnum

 filename = '.\mynewdir\' 
 
 Inquire(DIRECTORY=trim(filename),EXIST=FEXIST) !查询文件夹的状态
 if(.not.FEXIST) then  !如果不存在,则新建该文件夹
   res=MAKEDIRQQ(trim(filename))
   write(*,*) 'New folder created'
 else
   command ='rm -r '//trim(filename)  !否则,则用系统命令删除该文件夹,及该文件下的所有文件。

!注意,Fortran自带命令DELDIRQQ 只能删除空文件夹
write(,) command

   STATUS = SYSTEM(command)
   If (STATUS .eq. -1) then  !如果出错,显示出错信息
     errnum = ierrno( )
   print *, 'Error ', errnum
   else
      write(*,*) 'the folder '//trim(filename)//' is deleted'
   end if
   res=MAKEDIRQQ(trim(filename)) !先删除,然后再新建该文件夹
endif

end

你可能感兴趣的:(学习经验,其他)