sys_copy_dir Subroutine

public subroutine sys_copy_dir(source, dest, success, error_msg)

Copy a directory recursively from source to destination

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: source
character(len=*), intent(in) :: dest
logical, intent(out) :: success
character(len=*), intent(out), optional :: error_msg

Source Code

    subroutine sys_copy_dir(source, dest, success, error_msg)
        character(len=*), intent(in) :: source, dest
        logical, intent(out) :: success
        character(len=*), intent(out), optional :: error_msg
        character(len=512) :: command
        integer :: exitstat

        if (get_os_type() == OS_WINDOWS) then
            command = 'xcopy /E /I /Y "'//trim(escape_shell_arg(source))//'" "'//trim(escape_shell_arg(dest))//'" >nul 2>&1'
        else
            command = 'cp -r "'//trim(escape_shell_arg(source))//'" "'//trim(escape_shell_arg(dest))//'" 2>/dev/null'
        end if

        call execute_command_line(command, exitstat=exitstat)
        success = (exitstat == 0)

        if (present(error_msg)) then
            if (.not. success) then
                error_msg = "Failed to copy directory"
            else
                error_msg = ""
            end if
        end if
    end subroutine sys_copy_dir