Fortran Coder

查看: 20817|回复: 24
打印 上一主题 下一主题

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

[复制链接]

83

帖子

35

主题

0

精华

专家

F 币
343 元
贡献
215 点
跳转到指定楼层
楼主
发表于 2017-9-12 07:29:19 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
求问????????????
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

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

QQ
沙发
发表于 2017-9-12 07:58:48 | 只看该作者
ivf编译器的话,可以参考
http://doinfolder.w.fcode.cn/
这个代码,然后递归。

83

帖子

35

主题

0

精华

专家

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

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

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

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

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

83

帖子

35

主题

0

精华

专家

F 币
343 元
贡献
215 点
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

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

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

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

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

7#
发表于 2017-9-13 11:12:43 | 只看该作者
Windows平台的话,调用dir命令并写入指定文件
dir /S/B > a.txt
具体参考下面的链接:
http://blog.csdn.net/wubai250/article/details/7732822

83

帖子

35

主题

0

精华

专家

F 币
343 元
贡献
215 点
8#
 楼主| 发表于 2017-9-13 11:17:47 | 只看该作者
pasuka 发表于 2017-9-13 11:12
Windows平台的话,调用dir命令并写入指定文件
dir /S/B > a.txt
具体参考下面的链接:

获得文件名后我还想进一步操作,所以编程比较方便

83

帖子

35

主题

0

精华

专家

F 币
343 元
贡献
215 点
9#
 楼主| 发表于 2017-9-13 11:52:15 | 只看该作者
kyra 发表于 2017-9-13 10:36
callBack 是接口,ToDoOneFile 是它的真实实现。

[mw_shl_code=fortran,true]Program www_fcode_cn

感谢!!!

好人帮到底吧,想把每个文件重命名为前10个字符,低于十个字符文件名不变,后缀名不变该怎么弄

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

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

QQ
10#
发表于 2017-9-13 12:23:25 | 只看该作者
这中间涉及到很多问题,比如重名怎么办?
比如

有2个文件,分别叫
1234567890123.txt
1234567890124.txt


还有中文字符怎么办?比如
我是1个中文文件名.txt

等等。

如果你只是重命名,我觉得批处理比较适合你。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-4-19 21:45

Powered by Tencent X3.4

© 2013-2024 Tencent

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