在Fortran代码中,在指定路径中自动创建文件夹。首先,使用内部函数“inquire”查询,指定路径中将要创建的文件夹是否存在;然后,根据“inquire”反馈的结果,用系统命令中创建文件夹的命令进行相应文件夹的自动创建。另外,在Fortran中,若要输入单引号或者双引号,则需连续输入两次单引号或双引号即可。
示例代码:
! 输出路径
!----------------
module Path
implicit none
character(len=100) :: out_path="D:\Code\Routine Code\Interface\2016-01-29\"
!- 查询目录文件夹状态或使用目录文件夹,允许字符串中存在空格
character(len=100) :: out_path1="D:\Code\""Routine Code""\Interface\2016-01-29\"
!- 调用系统命令自动创建目录文件夹,字符串中空格需用引号处理
end module Path
!----------------
module Path
implicit none
character(len=100) :: out_path="D:\Code\Routine Code\Interface\2016-01-29\"
!- 查询目录文件夹状态或使用目录文件夹,允许字符串中存在空格
character(len=100) :: out_path1="D:\Code\""Routine Code""\Interface\2016-01-29\"
!- 调用系统命令自动创建目录文件夹,字符串中空格需用引号处理
end module Path
program Main
use Path
implicit none
character(len=100) :: path_result,path_result0,path_result11,path_result10
real(kind=4) :: Time_cost,TA(2),ETIME
!- 内部函数ETIME长度只能是4字节,即kind=4,见Intel Visual Fortran帮助文件
!...调用系统命令自动创建目录文件夹(计算结果输出目录)
!------------------------------------------
print*,'Check and creat the file directionary: '
print*,' '
path_result=trim(adjustl(out_path))//"Result\"
path_result0=trim(adjustl(out_path1))//"Result\"
call Createfolder(path_result,path_result0)
use Path
implicit none
character(len=100) :: path_result,path_result0,path_result11,path_result10
real(kind=4) :: Time_cost,TA(2),ETIME
!- 内部函数ETIME长度只能是4字节,即kind=4,见Intel Visual Fortran帮助文件
!...调用系统命令自动创建目录文件夹(计算结果输出目录)
!------------------------------------------
print*,'Check and creat the file directionary: '
print*,' '
path_result=trim(adjustl(out_path))//"Result\"
path_result0=trim(adjustl(out_path1))//"Result\"
call Createfolder(path_result,path_result0)
!...程序运行所耗总时间
!------------------------------------------
write(*,'(/)')
Time_cost=ETIME(TA)
write(*,*) 'Program has used',Time_cost,'seconds of CPU time.'
write(*,*) 'This includes',TA(1),'seconds of user time and', &
TA(2),'seconds of system time.'
write(*,*) ' '
stop ' Calculation is over'
end program Main
!------------------------------------------
write(*,'(/)')
Time_cost=ETIME(TA)
write(*,*) 'Program has used',Time_cost,'seconds of CPU time.'
write(*,*) 'This includes',TA(1),'seconds of user time and', &
TA(2),'seconds of system time.'
write(*,*) ' '
stop ' Calculation is over'
end program Main
! 创建目录文件夹路径
!-----------------------
subroutine Createfolder(path_inquire,path_create)
use IFPORT
use Path
implicit none
integer(kind=4) :: istatus1,errnum
logical(kind=4) :: ierr1
character(len=100) :: path_inquire,path_create
inquire(DIRECTORY=trim(adjustl(path_inquire)), EXIST=ierr1)
print*,ierr1
if(ierr1) then
print*,'The directory have existed and not been needed to create'
write(*,'(/)')
else
print*,'The directory not exist and creat it'
write(*,'(/)')
istatus1=SYSTEM('md '//trim(adjustl(path_create)))
if(istatus1==-1) then
errnum=ierrno()
print*,'Error=',errnum,' inquire the Intel Visual Fortran help document'
print*,' '
stop ' Folder creating is fail'
end if
end if
return
end subroutine Createfolder
!-----------------------
subroutine Createfolder(path_inquire,path_create)
use IFPORT
use Path
implicit none
integer(kind=4) :: istatus1,errnum
logical(kind=4) :: ierr1
character(len=100) :: path_inquire,path_create
inquire(DIRECTORY=trim(adjustl(path_inquire)), EXIST=ierr1)
print*,ierr1
if(ierr1) then
print*,'The directory have existed and not been needed to create'
write(*,'(/)')
else
print*,'The directory not exist and creat it'
write(*,'(/)')
istatus1=SYSTEM('md '//trim(adjustl(path_create)))
if(istatus1==-1) then
errnum=ierrno()
print*,'Error=',errnum,' inquire the Intel Visual Fortran help document'
print*,' '
stop ' Folder creating is fail'
end if
end if
return
end subroutine Createfolder