search in the directory and under directory

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Julien Morel.

Hello,
I want to search all the *.txt files which are in the directory and under directory
How to make?
Somebody can help me ?

Thanks

Forum in French
http://www.forumpurebasic.fr.st
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Sebi.

You can make this with ExamineDirectory() and some other commands.
Here two examples:

1.Example how to find only one file:

Code: Select all

Procedure.s SeekFile(directory.s , filename.s, extension.s , directoryid.l )  If Right(directory,1)"\"
    directory+"\"
  EndIf
  ExamineDirectory(directoryid,directory,"*.*")
  dirid=NextDirectoryEntry()
  While dirid
    Select dirid
      Case 1
        file.s=DirectoryEntryName()
        this_extension.s=LCase(GetExtensionPart(file))
        this_filename.s=LCase(Left(file,Len(file)-Len(this_extension)))
        If Len(this_extension)
          this_filename=Left(this_filename,Len(this_filename)-1)
        EndIf
        If this_extension=extension Or extension="*"
          If this_filename=filename Or filename="*"
            file.s=directory+DirectoryEntryName()    
            ProcedureReturn file
          EndIf
        EndIf
      Case 2
        If DirectoryEntryName()"." And DirectoryEntryName()".."     
          file.s=SeekFile(directory+DirectoryEntryName()+"\",filename,extension,directoryid+1)      
          UseDirectory(directoryid)
          If file""
            ProcedureReturn file
          EndIf
        EndIf
    EndSelect
    dirid=NextDirectoryEntry()
  Wend
  ProcedureReturn ""
EndProcedure

Debug "kernel32.dll is at"
Debug SeekFile("c:\","kernel32","dll",0)
Debug ""
2. Example how to search all *.txt-files:

Code: Select all

Procedure.s ListFiles(directory.s , filename.s, extension.s , directoryid.l )
  If Right(directory,1)"\"
    directory+"\"
  EndIf
  ExamineDirectory(directoryid,directory,"*.*")
  dirid=NextDirectoryEntry()
  While dirid
    Select dirid
      Case 1
        file.s=DirectoryEntryName()
        this_extension.s=LCase(GetExtensionPart(file))
        this_filename.s=LCase(Left(file,Len(file)-Len(this_extension)))
        If Len(this_extension)
          this_filename=Left(this_filename,Len(this_filename)-1)
        EndIf
        If this_extension=extension Or extension="*"
          If this_filename=filename Or filename="*"
            file.s=directory+DirectoryEntryName()
            
            ;file contains the full path and filename
            ;you must insert here what to do with the file
            Debug file
                
          EndIf
        EndIf
      Case 2
        If DirectoryEntryName()"." And DirectoryEntryName()".."     
          ListFiles(directory+DirectoryEntryName()+"\",filename,extension,directoryid+1)      
          UseDirectory(directoryid)
        EndIf
    EndSelect
    dirid=NextDirectoryEntry()
  Wend
  ProcedureReturn ""
EndProcedure

Debug "all txt-files in c:\windows\"
ListFiles("c:\windows","*","txt",0)
Debug ""
In both examples the last parameter should be zero.

Sebi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Julien Morel.

A very big thanks, that much will help me!!

Thanks



Forum in French
http://www.forumpurebasic.fr.st
Post Reply