shrine 发表于 2017-9-12 07:29:19

请问怎么获得包括子文件夹的所有文件名

求问????????????

vvt 发表于 2017-9-12 07:58:48

ivf编译器的话,可以参考
http://doinfolder.w.fcode.cn/
这个代码,然后递归。

shrine 发表于 2017-9-12 14:13:19

本帖最后由 shrine 于 2017-9-12 21:28 编辑

vvt 发表于 2017-9-12 07:58
ivf编译器的话,可以参考
http://doinfolder.w.fcode.cn/
这个代码,然后递归。
如何判断是文件夹,然后进入到文件夹,遍历后再回到上一层目录,继续下一个文件夹?

kyra 发表于 2017-9-13 07:24:58

If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
call CallBack( Trim(stInfo%Name) , iTotal + 1 )
iTotal = iTotal + 1
Else
call DoWithWildcard( ... ) !!!文件夹,递归
End If

shrine 发表于 2017-9-13 08:07:53

本帖最后由 shrine 于 2017-9-13 08:46 编辑

kyra 发表于 2017-9-13 07:24
If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
call CallBack( Trim(stIn ...

是这样吗?不行啊

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

kyra 发表于 2017-9-13 10:36:37

callBack 是接口,ToDoOneFile 是它的真实实现。

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

pasuka 发表于 2017-9-13 11:12:43

Windows平台的话,调用dir命令并写入指定文件
dir /S/B > a.txt
具体参考下面的链接:
http://blog.csdn.net/wubai250/article/details/7732822

shrine 发表于 2017-9-13 11:17:47

pasuka 发表于 2017-9-13 11:12
Windows平台的话,调用dir命令并写入指定文件
dir /S/B > a.txt
具体参考下面的链接:


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

shrine 发表于 2017-9-13 11:52:15

kyra 发表于 2017-9-13 10:36
callBack 是接口,ToDoOneFile 是它的真实实现。

Program www_fcode_cn


感谢!!!

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

kyra 发表于 2017-9-13 12:23:25

这中间涉及到很多问题,比如重名怎么办?
比如

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

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

等等。

如果你只是重命名,我觉得批处理比较适合你。
页: [1] 2 3
查看完整版本: 请问怎么获得包括子文件夹的所有文件名