loopsdirectoryvisual-foxprofoxpro

How to loop through nested directories in Foxpro given the initial start directory


I am trying to loop through a local directory and print out the name of all subdirectories and files within. The start path of the search is provided, and it can be possible that there are nested directories.

The start directory looks like:

enter image description here

and for example, within the appendix directory, we have:

enter image description here

And the directory nesting could go on as many times, but for this example these two sub directories a1 and a2 only contain some .txt files.

I have seen some other similar question like Get list Of Files in a Directory in Foxpro but I do not seem to get anything correct with what is shown here. If this question and answer does indeed apply directly to my question maybe someone can help clarify how I can utilize this answer.

Right now I just want to print out the names of all content (directories and files) as the loop goes through each subdirectory until it has gone through all items inside the initial starting directory. It would also be nice to print out other information like the last modified date of each item as well. The output with just the file names would look something like:

appendix

a1

a1text.txt

a2

a2text.txt

aTotal.txt

index (nothing inside index so moving on)

names

allnames.txt

Thanks in advance for any help.


Solution

  • When you run the following sample code, it evaluates all File items in the parametrized start folder, and when it finds a sub-Folder, there it starts itself recursively to do the same again.

    As for your comments asking for additional explanations: The local F1 Help file that ships with visual-foxpro, i.e. its community version content, is also available online:

    vfphelp-adir

    The ADir() function places information about files into an array and then returns the number of files. 1: File names 2: File sizes 3: Dates last modified 4: Times last modified 5: File attributes

    vfphelp-directory

    The Directory() function checks whether a string is an existing File System folder name

    vfphelp-addbs

    The AddBS() function adds a trailing backslash to a string if there is none yet

    Don't hesitate to comment on additional aspects.

    * creating a temp. table to store and show the results
    CREATE CURSOR filesandfolders (cpath C(254), cfile C(254))
    INDEX on LEFT(cpath,120) + LEFT(cfile,120) TAG myorder
    
    GetFilesRecursively(HOME())
    *!* GetFilesRecursively(HOME(),.T.)
    
    GO TOP IN filesandfolders
    BROWSE WIDTH 60
    RETURN
    
    PROCEDURE GetFilesRecursively(tcFolder, tlNoSubfolders)
        ASSERT ( VARTYPE(m.tcFolder) = 'C' )
        ASSERT ( VARTYPE(m.tlNoSubfolders) = 'L' )
    
        LOCAL lnFile, laFiles[1], lcFileOrFolder
        FOR lnFile = 1 TO ADIR(laFiles, ADDBS(m.tcFolder)+'*', 'D')
            lcFileOrFolder = LOWER(laFiles[m.lnFile, 1])
            IF EMPTY( STRTRAN(m.lcFileOrFolder,'.') )
                LOOP
            ENDIF
    
            IF DIRECTORY( ADDBS(m.tcFolder)+m.lcFileOrFolder, 1 )
                IF !m.tlNoSubfolders
                    GetFilesRecursively(ADDBS(m.tcFolder)+m.lcFileOrFolder)
                ENDIF
            ELSE
                INSERT INTO filesandfolders VALUES ( ;
                    m.tcFolder, m.lcFileOrFolder )
            ENDIF
        ENDFOR
    ENDPROC
    

    Result image

    Edited: As for the OP's comment:

    lcFileOrFolder values are . or .. Why does adir() create values like this?

    The dots get into the first ADir() result elements when its 3rd parameter is "D"(irectory). IDE Command Window example:

    CD HOME()
    ? ADIR(laDir, "*")
    ? laDir[1,1] && no "D" no dots, first element is a file name, e.g."Beautify.APP"
    ? ADIR(laDir, "*", "D")
    ? laDir[1,1] && dot "."
    ? laDir[2,1] && double dot ".."
    

    That behavior is not documented AFAIK, might perhaps have been an ancient feature related to the DOS/FoxPro CD command