3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Fortran】指定したパスからディレクトリを作成

Last updated at Posted at 2019-05-29

流れ

  • 与えられた文字列がディレクトリなのか,ファイル名まで含むのかを判定
  • ファイル名まで含む場合,ディレクトリ部分のみを取り出す
  • ディレクトリ作成

文字列の末尾の判定

Pythonで言うstr.endswith()

endswith.f90
module mymod
    implicit none
contains

function endswith(longtext, tailtext) result(ends)
    logical ends
    character(len=*), intent(in) :: longtext, tailtext
    character(len=256) longtext_, tailtext_
    integer long_eloc, tail_eloc

    ends = .FALSE.
    longtext_ = trim(longtext)
    tailtext_ = trim(tailtext)
    long_eloc = len_trim(longtext)
    tail_eloc = len_trim(tailtext)
    if (long_eloc == 0 .or. tail_eloc == 0) return
    if (long_eloc < tail_eloc) return
    if (longtext_(long_eloc - tail_eloc + 1:) == tailtext_) then
        ends = .TRUE.
    endif
end function endswith

end module mymod

program main
    use mymod
    implicit none
    write(*, *) endswith('abc' , 'c' ) ! T
    write(*, *) endswith('abc' , 'bc') ! T
    write(*, *) endswith('abc' , 'a' ) ! F
    write(*, *) endswith('abc' , 'ac') ! F
    write(*, *) endswith('abc ', 'c' ) ! T
    write(*, *) endswith('abc/', '/' ) ! T
end program main

パスからディレクトリの取り出し

Pythonで言うos.path.dir_name()

dir_name
module mymod
    implicit none
contains

function dir_name(path) result(dir)
    character(len=256) dir
    character(len=*), intent(in) :: path
    integer iloc
    logical is_found

    if (endswith(path, '/')) then
        dir = path
        return
    endif

    is_found = .FALSE.
    do iloc = len_trim(path), 1, -1
        if (path(iloc:iloc) == '/') then
            is_found = .TRUE.
            exit
        endif
    enddo

    if (is_found) then
        dir = path(:iloc)
    else
        dir = path
    endif
end function dir_name

end module mymod

program main
    use mymod
    implicit none
    write(*, *) trim(dir_name('abc/de'))   ! abc/
    write(*, *) trim(dir_name('abc/'))     ! abc/
    write(*, *) trim(dir_name('abc/de  ')) ! abc/
    write(*, *) trim(dir_name('abc/  '))   ! abc/
    write(*, *) trim(dir_name('/abc/'))    ! /abc/
    write(*, *) trim(dir_name('/abc/de'))  ! /abc/
end program main

1文字取り出すのはtext(1)ではなくtext(1:1).
http://www.nag-j.co.jp/fortran/FI_13.html

ディレクトリの作成はcall system()

Pythonで言うos.makedirs()

mkdir.f90
module mymod
    implicit none
contains

subroutine makedirs(outdir)
    character(len=*), intent(in) :: outdir
    character(len=256) command
    write(command, *) 'if [ ! -d ', trim(outdir), ' ]; then mkdir -p ', trim(outdir), '; fi'
    write(*, *) trim(command)
    call system(command)
end subroutine makedirs

end module mymod

program main
    use mymod
    implicit none
    call makedirs('./test/sub')
    call makedirs('./test/sub')
end program main

https://qiita.com/aisha/items/5ce5971a4c179ac83c6e#%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88%E3%83%AA%E3%81%AE%E4%BD%9C%E6%88%90%E3%81%AFcall-system%E3%81%A7
https://sites.google.com/site/fcfortran/home/recipe/mkdir

まとめ

以上のサブルーチンを利用すると,

call makedirs(dir_name(path)) ! pathは出力したいファイルのパス

と1行でディレクトリ作成可能.

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?