Fortran Coder

楼主: shrine
打印 上一主题 下一主题

[绘图界面库] 请问怎么获得包括子文件夹的所有文件名

[复制链接]

255

帖子

0

主题

0

精华

版主

World Analyser

F 币
699 元
贡献
502 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
6#
发表于 2017-9-13 10:36:37 | 只看该作者
callBack 是接口,ToDoOneFile 是它的真实实现。

[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
   Implicit None
   integer :: n = 0
   External ToDoOneFile
   call DoWithWildcard( "C:\dosh\*" , ToDoOneFile , n )
   write(*,*) '共',n,'个文件'
End Program www_fcode_cn

Subroutine ToDoOneFile( cFile , iLoop )
   Character( Len = * ) , Intent( IN ) :: cFile
   Integer , Intent( IN ) :: iLoop
   Write( * , * ) '第',iLoop,'个文件:',cFile
   !Open( 12 , File = cFile )
   !Read( 12 )
   !Close( 12 )
End Subroutine ToDoOneFile
   
Recursive Subroutine DoWithWildcard(cWildcard,CallBack,iTotal)
   !// 下一句代码,如果是 Compaq 或 Digital,需改为 Use DFLib
   Use IFPort , only : GetFileInfoQQ , GetLastErrorQQ , FILE$INFO , FILE$LAST , FILE$ERROR , FILE$FIRST , ERR$NOMEM , ERR$NOENT , FILE$DIR
   Implicit None
   Interface 
     Subroutine CallBack( cFile , iLoop )
       Character( Len = * ) , Intent( IN ) :: cFile
       Integer , Intent( IN ) :: iLoop
     End Subroutine CallBack
   End Interface
   Character( Len = * ) , Intent( IN ) :: cWildcard
   Integer , Intent( OUT ) :: iTotal
   Type (FILE$INFO) :: stInfo
   Integer(4) :: iWildhandle , iLength , iRet
   iWildhandle = FILE$FIRST
   Do While (.TRUE.)
       iLength = GetFileInfoQQ( cWildCard , stInfo , iWildhandle )
       If (( iWildhandle == FILE$LAST) .OR.( iWildhandle == FILE$ERROR )) then
         Select Case (GetLastErrorQQ())
         Case (ERR$NOMEM)  !//内存不足
           iTotal = - 1
           return
         Case (ERR$NOENT)  !//碰到通配符序列尾
           return
         Case Default
           iTotal = 0
           return
         End Select
       End If
       iLength = index( cWildcard , "\" , .true. )
       If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
         call CallBack( cWildcard(:iLength)//trim(stInfo%Name) , iTotal + 1 )
         iTotal = iTotal + 1
       Else         
         if(stInfo%Name(1:1) /= "." ) then           
           call DoWithWildcard( cWildcard(:iLength)//trim(stInfo%Name)//"\"//cWildcard(iLength+1:) , CallBack , iTotal )
         end if
       End If
   End Do
 End Subroutine DoWithWildcard

86

帖子

36

主题

0

精华

专家

F 币
352 元
贡献
221 点
5#
 楼主| 发表于 2017-9-13 08:07:53 | 只看该作者
本帖最后由 shrine 于 2017-9-13 08:46 编辑
kyra 发表于 2017-9-13 07:24
[Fortran] 纯文本查看 复制代码
If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
  call CallBack( Trim(stIn ...

是这样吗?不行啊

[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
   Implicit None
   integer :: n
   External ToDoOneFile
   call DoWithWildcard( "j:\*.*" , ToDoOneFile , n )
   write(*,*) '共',n,'个文件'
End Program www_fcode_cn 

Subroutine ToDoOneFile( cFile , iLoop )
   Character( Len = * ) , Intent( IN ) :: cFile
   Integer , Intent( IN ) :: iLoop
   Write( * , * ) '第',iLoop,'个文件:',cFile
End Subroutine ToDoOneFile

recursive Subroutine DoWithWildcard(cWildcard,CallBack,iTotal)
   !// 下一句代码,如果是 Compaq 或 Digital,需改为 Use DFLib
   Use IFPort , only : GetFileInfoQQ , GetLastErrorQQ , FILE$INFO , FILE$LAST , FILE$ERROR , FILE$FIRST , ERR$NOMEM , ERR$NOENT , FILE$DIR
   Implicit None
   Interface
     Subroutine CallBack( cFile , iLoop )
       Character( Len = * ) , Intent( IN ) :: cFile
       Integer , Intent( IN ) :: iLoop
     End Subroutine CallBack
   End Interface
   Character( Len = * ) , Intent( IN ) :: cWildcard
   Integer , Intent( OUT ) :: iTotal
   Type (FILE$INFO) :: stInfo
   Integer(4) :: iWildhandle , iLength , iRet
   iWildhandle = FILE$FIRST
   iTotal = 0
   Do While (.TRUE.)
       iLength = GetFileInfoQQ( cWildCard , stInfo , iWildhandle )
       If (( iWildhandle == FILE$LAST) .OR.( iWildhandle == FILE$ERROR )) then
         Select Case (GetLastErrorQQ())
         Case (ERR$NOMEM)  !//内存不足
           iTotal = - 1
           return
         Case (ERR$NOENT)  !//碰到通配符序列尾
           return
         Case Default
           iTotal = 0
           return
         End Select
       End If
       
    If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
      call CallBack( Trim(stInfo%Name) , iTotal + 1 )
      iTotal = iTotal + 1
    Else
      call DoWithWildcard( cWildcard , CallBack , iTotal ) !!!文件夹,递归
    End If
   End Do

 End Subroutine DoWithWildcard


另外问一下,CallBack和ToDoOneFile什么关系?没有用过interface

255

帖子

0

主题

0

精华

版主

World Analyser

F 币
699 元
贡献
502 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
地板
发表于 2017-9-13 07:24:58 | 只看该作者
[Fortran] 纯文本查看 复制代码
If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
  call CallBack( Trim(stInfo%Name) , iTotal + 1 )
  iTotal = iTotal + 1
Else
  call DoWithWildcard( ... ) !!!文件夹,递归
End If

86

帖子

36

主题

0

精华

专家

F 币
352 元
贡献
221 点
板凳
 楼主| 发表于 2017-9-12 14:13:19 | 只看该作者
本帖最后由 shrine 于 2017-9-12 21:28 编辑
vvt 发表于 2017-9-12 07:58
ivf编译器的话,可以参考
http://doinfolder.w.fcode.cn/
这个代码,然后递归。

如何判断是文件夹,然后进入到文件夹,遍历后再回到上一层目录,继续下一个文件夹?

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2017-9-12 07:58:48 | 只看该作者
ivf编译器的话,可以参考
http://doinfolder.w.fcode.cn/
这个代码,然后递归。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-19 23:29

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表