[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
Implicit None
integer :: n
External ToDoOneFile
call DoWithWildcard( "j:\123\*" , 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
Subroutine ToDoOneFile( cFile , iLoop )
use , intrinsic :: ISO_C_Binding
INTERFACE
FUNCTION GetShortPathName(lpszLongPath,lpszShortPath,cchBuffer)
import
integer :: GetShortPathName ! DWORD
!DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'GetShortPathNameA' :: GetShortPathName
!DEC$ ATTRIBUTES REFERENCE, ALLOW_NULL :: lpszLongPath
character*(*) lpszLongPath ! LPCSTR lpszLongPath
!DEC$ ATTRIBUTES REFERENCE, ALLOW_NULL :: lpszShortPath
character*(*) lpszShortPath ! LPSTR lpszShortPath
integer cchBuffer ! DWORD cchBuffer
END FUNCTION
END INTERFACE
Character( Len = * ) , Intent( IN ) :: cFile
Integer , Intent( IN ) :: iLoop
integer :: k
character(len=512) :: sFile
k = GetShortPathName( trim(cFile)//c_null_char , sFile , len(sFile) )
Write( * , * ) '第',iLoop,'个文件:', sFile(:k)
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
!character(len=256) a
integer::zl
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
iLength = index( cWildcard , "\" , .true. )
If ( ( stInfo%permit.AND.FILE$DIR ) == 0 ) then
call CallBack( cWildcard(:iLength)//trim(stInfo%Name) , iTotal + 1 )
iTotal = iTotal + 1
!zl=len(stInfo.Name)
!write(*,*)stInfo.Name
!write(*,*)zl
Else
if(stInfo%Name(1:1) /= "." ) then
call DoWithWildcard( cWildcard(:iLength)//trim(stInfo%Name)//"\"//cWildcard(iLength+1:) , CallBack , iTotal ) !!!文件夹,递归
endif
End If
End Do
End Subroutine DoWithWildcard