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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值