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

Make a subdirectory in F90

TommyCee
Beginner
3,134 Views
Does anyone know whether it's possible to create ("open") a subdirectory in the working directory of a project at runtime? What I'd like to do is, early in the program, create the subdirctory if it's non-existent. If it already exists (e.g., from an earlier execution), ignore and move on.

Output files will then opened on a path that includes this subdirctory.

I researched this and did not see any such possibility.
0 Kudos
8 Replies
Brian_Allison
Beginner
3,134 Views
if not exist (dir) mkdir (dir)
Make sure to tab this entry
0 Kudos
Steven_L_Intel1
Employee
3,134 Views
MAKEDIRQQ - see the description in the Language Reference.
0 Kudos
TommyCee
Beginner
3,134 Views
Could I please get a bit more help. Some syntax?
I find no refs for the command mkdir in any of my Visual Fortran manuals.

The line:
mkdir (Output)

and

if (.not exist. (Output)) mkdir (Output)

trigger an error:
Syntax error, found END-OF-STATEMENT when expecting one of: ( % . = =>
0 Kudos
mecej4
Honored Contributor III
3,134 Views
You need to recognize that concepts such as directories are external to Fortran. Therefore, there are no "commands" or "verbs" in Fortran to manipulate or enquire about directories.

However, compilers usually provide Fortran callable library routines to provide a bridge between Fortran code and operating system services. These libraries are compiler/OS specific, and are described in the compiler documentation, not in a Fortran language reference book.
0 Kudos
Steven_L_Intel1
Employee
3,134 Views
The suggestion for using "mkdir" would mean that you'd have to use SYSTEMQQ or RUNQQ to executa a command line. You don't need to do that - as I wrote above, MAKEDIRQQ does exactly what you want.

USE IFPORT
if (.not. MAKEDIRQQ("subdir")) then
print *, "Failed to create subdirectory"
end if
0 Kudos
TommyCee
Beginner
3,134 Views
Thanks Steve - that's just the nudge what I needed. That was huge!

[bash]use DFLIB
LOGICAL(4) result
result = MakeDirQQ('mynewdir')

IF (result) THEN WRITE (*,*) 'New subdirectory successfully created' ELSE WRITE (*,*) 'Failed to create subdirectory' END IF END[/bash]
0 Kudos
Les_Neilson
Valued Contributor II
3,134 Views
You can also use the INQUIRE statement to see if the directory exists.

logical:: L_EXISTS
character(12) :: C_DIRSPEC
C_DIRSPEC = "subdirectory"
inquire(directory=C_DIRSPEC, exist=L_EXISTS)

if(.not.L_EXISTS) then
print *,"The directory ",C_DIRSPEC," does not exist"
endif

Les

0 Kudos
Steven_L_Intel1
Employee
3,134 Views
Note that the DIRECTORY keyword to INQUIRE is our extension to the standard.
0 Kudos
Reply