Page 1 of 1

List of all the files in a TXT file

Posted: Sat Jan 03, 2004 7:35 pm
by Polo
I'm currently trying to do some crazy things in Pure, to see what it can do, and I would like to know if someone know :) how to do a list of all the files in a folder and to put this list into a txt file :)
It would be great to see if someone can code that !

Posted: Sat Jan 03, 2004 7:42 pm
by LarsG
Why not try to do it yourself?! It should be fairly easy..

Just read some up on files and directories in the helpfile..

The best way to learn something is to try it yourself...
Give it a go, and I'm sure someone (if not me) can help you if you're stuck!
The most important thing is that you try it yourself..

-Lars

Posted: Sat Jan 03, 2004 7:42 pm
by blueznl
i posted something like that a while ago, a little sub that dumps all files in an array, use the search function and thy shall find...

Posted: Sat Jan 03, 2004 7:42 pm
by Guimauve
PathRequester()
ExamineDirectory()
DirectoryEntryName()
NextDirectoryEntry()

I think you can create a small list of all files in any directory with these commands.

Good luck !!

Posted: Sat Jan 03, 2004 7:43 pm
by Num3
:twisted:

Code: Select all

Dir *.* > list.txt

Posted: Sat Jan 03, 2004 7:43 pm
by LarsG
wow.. three replies within the same minute.. :D
*edit* plus one a few seconds later.. lol *edit*

Posted: Sat Jan 03, 2004 7:44 pm
by blueznl
we're good... real good...

:lol:

Posted: Sat Jan 03, 2004 7:46 pm
by LarsG
blueznl wrote:we're good... real good...

:lol:
yeah.. except Num3.. he's just evil.. LOL

Posted: Sat Jan 03, 2004 7:56 pm
by Henrik
Allright guyes you'r not nice :wink:

if you'r completly stuck Polo

try "FileSystem.pb" from the Examples folder
And see if you can finde appropriate plase to add these 3 lines

CreateFile(1, "Test.txt")
WriteStringN(FileName$)
CloseFile(1)

Henrik

Posted: Sat Jan 03, 2004 8:09 pm
by LarsG
Henrik wrote:Allright guyes you'r not nice :wink:

if you'r completly stuck Polo

try "FileSystem.pb" from the Examples folder
And see if you can finde appropriate plase to add these 3 lines

CreateFile(1, "Test.txt")
WriteStringN(FileName$)
CloseFile(1)

Henrik
It's always best to try to solve problems for oneself... 8)

Posted: Sat Jan 03, 2004 8:22 pm
by Polo
Wow ! I never thought I would find a way so quickly ! Thanks a lot !

Posted: Sat Jan 03, 2004 8:27 pm
by Kale
Ok, ok, i know it can be hard starting to understand a new language so here is what you wanted:

Code: Select all

;Declare some variables
IncludeDirectoriesInListings.l = #FALSE
DirectoryName.s
FileType.l

;Create a new LinkedList that will hold the results of our directory 'scan'
NewList Listings.s()

;Choose a directory
DirectoryName = PathRequester("Please choose a folder...", "")

;Examine that directory
If ExamineDirectory(1, DirectoryName, "") ; If directory can be read...
    Repeat
        FileType = NextDirectoryEntry()
        If FileType = 1 ; 1 = File
            ;Add the file name to the listings
            AddElement(Listings())
            Listings() = DirectoryEntryName()
        EndIf
        If IncludeDirectoriesInListings = #TRUE ; only include folder names within the choosen directory if 'IncludeDirectoriesInListings = #TRUE'
            If FileType = 2 ; 2 = Folder
                ;Add the folder name to the listings
                AddElement(Listings())
                Listings() = DirectoryEntryName()
            EndIf
        EndIf
    Until FileType = 0 
EndIf

;write the listings to a file
If OpenFile(1, "Listings.txt") ; If file can be created...
    ForEach Listings()
        WriteStringN(Listings())
    Next
    CloseFile(1)
EndIf
Simple :D

Posted: Sat Jan 03, 2004 8:31 pm
by Polo
thanks !

And is there a way to sort them by name ?

Posted: Sun Jan 04, 2004 12:33 am
by blueznl
(did you use the search?) all below could be found here: viewtopic.php?t=8359&highlight=xdir

kale's is shorter, but i sort it :-) and i think (not sure, haven't tested it) that mine is a little faster, though uglier :-)

short version...

Code: Select all

; 
  path.s = "c:\windows" 
  ; 
  If list_limit.l = 0 
    list_limit = 20000                              ; arbitrary limit 
  EndIf 
  ; 
  If Right(path.s,1)<>"\" 
    path = path + "\" 
  EndIf 
  ; 
  Dim x_dir_list.s(list_limit) 
  folder_n = 1 
  x_dir_list(list_limit-folder_n) = path            ; store paths back to front 
  file_n = 0 
  ; 
  While folder_n > 0 
    folder.s = x_dir_list(list_limit-folder_n) 
    x_dir_list(list_limit-folder_n)=""              ; << comment out when done debugging 
    folder_n = folder_n-1 
    If ExamineDirectory(nr,folder,"*.*")  
      file_added = 0      
      folder_added = 0 
      ; 
      ; add files (bottom up) and folders (top down) 
      ; 
      Repeat 
        type.l = NextDirectoryEntry() 
        If Left(DirectoryEntryName(),1)="." 
        ElseIf type = 1 
          file_added = file_added+1 
          x_dir_list(file_n+file_added) = folder+DirectoryEntryName() 
        ElseIf type = 2 
          folder_added = folder_added+1 
          x_dir_list(list_limit-folder_n-folder_added) = folder+DirectoryEntryName()+"\" 
        Else 
        EndIf 
        If (folder_n+file_n+folder_added+file_added) >= list_limit-4    ; too many entries for the list 
          limit_reached = #True 
        EndIf 
      Until type = 0 Or limit_reached = #True 
      ; 
      ; if there are any new entries, sort them 
      ; 
      If file_added > 0 
        SortArray(x_dir_list(),2,file_n+1,file_n+file_added) 
        file_n = file_n+file_added 
      EndIf 
      If folder_added > 0 
        SortArray(x_dir_list(),2,list_limit-folder_n-folder_added,list_limit-folder_n-1) 
        folder_n = folder_n+folder_added 
      EndIf 
      ; 
    EndIf 
  Wend 
  ; 
  For n = 1 To file_n 
    Debug " "+x_dir_list(n) 
  Next n 
  Debug limit_reached

long version... (you'll need the file x_lib.pb from my site)

Code: Select all

Procedure.l x_matchpattern(string.s,pattern.s,ulcase.l) 
  Protected s_l, s_p, m_l, m_p, nomatch.l, match.l, ff.l, m.s 
  ; 
  ; *** check if string matches pattern 
  ; 
  ; in:     string.s   - string to check 
  ;         pattern.s  - pattern including wildcards * and ? and multiple patterns seperated by | 
  ;         ulcase.l   - 0 check case 1 don't care about case 
  ; retval: 0          - no match 
  ;         1          - match 
  ; 
  If ulcase.l = 1 
    string.s = LCase(string.s) 
    pattern.s = LCase(pattern.s) 
  EndIf 
  ; 
  s_l = Len(string) 
  s_p = 1 
  m_l = Len(pattern) 
  m_p = 0 
  ; 
  nomatch.l = #False 
  match.l = #False 
  ff.l = #False 
  While m_p < m_l And match = #False 
    m_p = m_p+1 
    m.s = Mid(pattern,m_p,1) 
    If m = "*" 
      If m_p = m_l 
        match = #True 
      Else 
        ff= #True 
      EndIf 
    EndIf 
    If m = "|" 
      If s_p = s_l+1 
        match = #True 
      EndIf 
    ElseIf m = "?" 
      If s_p <= s_l 
        s_p = s_p+1 
      Else 
        nomatch = #True 
      EndIf 
    ElseIf m <> "*" 
      If ff = #True 
        ff = #False 
        Repeat 
          s_p = s_p+1 
        Until s_p > s_l Or m=Mid(string,s_p,1) 
        If m = Mid(string,s_p,1) 
          s_p = s_p+1 
        Else 
          nomatch = #True 
        EndIf 
      Else 
        If m = Mid(string,s_p,1) 
          s_p = s_p+1 
        Else 
          nomatch = #True 
        EndIf 
      EndIf 
    EndIf 
    If nomatch = #False And s_p = s_l+1 
      match = #True 
    EndIf 
    If nomatch = #True 
      m_p = FindString(pattern,"|",m_p+1) 
      If m_p = 0 
        m_p = m_l 
      Else 
        nomatch = #False 
        ff = #False 
        s_p = 1 
      EndIf 
    EndIf 
  Wend 
  ; 
  ProcedureReturn match 
EndProcedure 

Procedure.l x_dir(path.s,pattern.s,mode.l,list_limit.l,drill_limit.l) 
  Protected folder.s, file_n.l, file_added.l , folder_n.l, folder_added.l, limit_reached.l, nr.l, deep.l 
  Global x_dir_n.l 
  ; 
  ; *** read contents from one or more subdirectories and store them in a linked list 
  ; 
  ; in:     path.s       - starting path 
  ;         pattern.s    - one or more patterns using wildcards * ? and seperator | 
  ;         mode.l       - 0 filename 
  ;                        1 filename and foldername 
  ;                        2 path+filename, explore subdirs 
  ;                        3 path+filename and path+foldername explore subdirs 
  ;                        4 filename and [foldername] 
  ;                        5 path+foldername followed by filenames 
  ;         list_limit   - maximal number of entries (use 0 for default: 50000) 
  ;         drill_limit  - number of levels (subdirs) to explore (use 0 for default: 50) 
  ; retval: >0           - number of entries found (listed subdirs count!) 
  ;         0            - nothing found 
  ;         <0           - hit list limit 
  ; out:    x_dir_list() - list with everything 
  ;         x_dir_n      - number of elements in the list 
  ; 
  ; 
  nr.l = x_nr()                                     ; or do nr.l = 1 if you don't use x_lib 
  ; 
  If list_limit.l = 0                               ; just for testing purposes :-) 
    list_limit = 40000                              ; arbitrary limit, was enough for my c: drive :-) 
  EndIf 
  If drill_limit.l = 0 
    drill_limit = 50                                ; arbitrary limit 
  EndIf 
  ; 
  If Right(path.s,1)<>"\" 
    path = path + "\" 
  EndIf 
  deep.l = x_countchar(path,"\") 
  ; 
  Dim x_dir_list.s(list_limit) 
  folder_n = 1 
  x_dir_list(list_limit-folder_n) = path            ; store paths bottom to top of array 
  file_n = 0 
  ; 
  While folder_n > 0 
    folder.s = x_dir_list(list_limit-folder_n) 
    ; x_dir_list(list_limit-folder_n)=""            ; << comment out when done debugging 
    folder_n = folder_n-1 
    ; 
    If x_countchar(folder,"\")-deep > drill_limit   ; don't go deeper 
    Else 
      If ExamineDirectory(nr,folder,"*.*")  
        If mode = 3 Or mode = 5 
          file_n = file_n+1 
          x_dir_list(file_n) = folder 
        EndIf 
        ; 
        file_added = 0      
        folder_added = 0 
        ; 
        ; add files (bottom up) and folders (top down) 
        ; 
        Repeat 
          type.l = NextDirectoryEntry() 
          If Left(DirectoryEntryName(),1)="." 
          ElseIf type = 1 
            If x_matchpattern(DirectoryEntryName(),pattern,1)=1 
              file_added = file_added+1 
              If mode = 0 Or mode = 4 Or mode = 5 
                x_dir_list(file_n+file_added) = DirectoryEntryName() 
              Else 
                x_dir_list(file_n+file_added) = folder+DirectoryEntryName() 
              EndIf 
            EndIf 
          ElseIf type = 2 
            If mode =0 
            ElseIf mode = 1 
              file_added = file_added+1 
              x_dir_list(file_n+file_added) = folder+DirectoryEntryName()+"\" 
            ElseIf mode=2 Or mode=3 Or mode=5 
              folder_added = folder_added+1 
              x_dir_list(list_limit-folder_n-folder_added) = folder+DirectoryEntryName()+"\" 
            ElseIf mode=4 
              file_added = file_added+1 
              x_dir_list(file_n+file_added) = "["+folder+DirectoryEntryName()+"]" 
            Else 
            EndIf 
          EndIf 
          If (folder_n+file_n+folder_added+file_added) >= list_limit-4    ; too many entries for the list 
            limit_reached = #True 
          EndIf 
        Until type = 0 Or limit_reached = #True 
        ; 
        ; if there are any new entries, sort them 
        ; 
        If file_added > 0 
          SortArray(x_dir_list(),2,file_n+1,file_n+file_added) 
          file_n = file_n+file_added 
        EndIf 
        If folder_added > 0 
          SortArray(x_dir_list(),2,list_limit-folder_n-folder_added,list_limit-folder_n-1) 
          folder_n = folder_n+folder_added 
        EndIf 
        ; 
      EndIf 
    EndIf 
  Wend 
  ; 
  x_freenr(nr) 
  ; 
  If limit_reached = #True 
    x_dir_n = 0 
    ProcedureReturn 0-file_n 
  Else 
    x_dir_n = file_n 
    ProcedureReturn file_n 
  EndIf 
EndProcedure


Posted: Sun Jan 04, 2004 10:58 am
by Polo
8O

Thanks to you all !