----------------------------------------------------------------------
--- Knud van Eeden --- 06 December 2009 - 01:04 am -------------------

Computer: Editor: Text: TSE: Directory: File: All: Operation: Search: How to find all occurrences of a given filename in a given directory and its subdirectories? [recursive walk the tree]

===

Steps: Overview:

 1. -E.g. create the following program:

E.g. create the following program:

--- cut here: begin --------------------------------------------------
// filenamemacro=searfitt.s
--- cut here: end ---------------------------------------------------- --- cut here: begin --------------------------------------------------
// PROC PROCFileSearchAllDirectoryWalkTheTree( STRING fileNameSearchS, STRING directoryS )
--- cut here: end ---------------------------------------------------- --- cut here: begin --------------------------------------------------
FORWARD PROC Main() FORWARD PROC PROCFileSearchAllDirectoryWalkTheTree( STRING s1, STRING s2, INTEGER i1 ) FORWARD PROC PROCFileSearchAllDirectoryWalkTheTreeSub( STRING s1, STRING s2, INTEGER i1 ) // --- MAIN --- // PROC Main() INTEGER bufferI = 0 STRING s1[255] = "ddd1.txt" STRING s2[255] = "c:\temp\ddd2" PushPosition() bufferI = CreateTempBuffer() PopPosition() IF ( ( Ask( "file: search: all: directory: walk: the: tree: searchS = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) IF ( ( Ask( "file: search: all: directory: walk: the: tree: directoryS = ", s2, _EDIT_HISTORY_ ) ) AND ( Length( s2 ) > 0 ) ) PROCFileSearchAllDirectoryWalkTheTree( s1, s2, bufferI ) ENDIF ENDIF END <F12> Main() // --- LIBRARY --- // // library: file: search: all: directory: walk: the: tree <description></description> <version control></version control> <version>1.0.0.0.11</version> (filenamemacro=searfitt.s) [kn, ri, su, 06-12-2009 00:51:06] PROC PROCFileSearchAllDirectoryWalkTheTree( STRING fileNameSearchS, STRING directoryS, INTEGER bufferI ) // e.g. PROC Main() // e.g. INTEGER bufferI = 0 // e.g. STRING s1[255] = "ddd1.txt" // e.g. STRING s2[255] = "c:\temp\ddd2" // e.g. PushPosition() // e.g. bufferI = CreateTempBuffer() // e.g. PopPosition() // e.g. IF ( ( Ask( "file: search: all: directory: walk: the: tree: searchS = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) // e.g. IF ( ( Ask( "file: search: all: directory: walk: the: tree: directoryS = ", s2, _EDIT_HISTORY_ ) ) AND ( Length( s2 ) > 0 ) ) // e.g. PROCFileSearchAllDirectoryWalkTheTree( s1, s2, bufferI ) // e.g. ENDIF // e.g. ENDIF // e.g. END // e.g. // e.g. <F12> Main() // PROCFileSearchAllDirectoryWalkTheTreeSub( fileNameSearchS, directoryS, bufferI ) // GotoBufferId( bufferI ) // END // library: file: search: all: directory: walk: the: tree: sub <description></description> <version control></version control> <version>1.0.0.0.0</version> (filenamemacro=searfits.s) [kn, ri, su, 06-12-2009 00:53:50] PROC PROCFileSearchAllDirectoryWalkTheTreeSub( STRING fileNameSearchS, STRING directoryS, INTEGER bufferI ) // e.g. PROC Main() // e.g. PROCFileSearchAllDirectoryWalkTheTreeSub( "ddd1.txt", "c:\temp\ddd1", CreateTempBuffer() ) // e.g. END // e.g. // e.g. <F12> Main() // INTEGER searchHandleI = FindFirstFile( Format( AddTrailingSlash( directoryS ), "*.*" ), -1 ) // STRING ffNameS[255] = "" // INTEGER directoryB = FALSE // // recursive stop // IF ( NOT ( searchHandleI == -1 ) ) // REPEAT // ffNameS = FFName() // directoryB = FALSE // IF ( FFAttribute() & _DIRECTORY_ ) // directoryB = TRUE // IF ( NOT ( ffNameS == "." ) AND ( NOT ( ffNameS == ".." ) ) ) // // recursive call // PROCFileSearchAllDirectoryWalkTheTreeSub( fileNameSearchS, Format( AddTrailingSlash( directoryS ), ffNameS ), bufferI ) // ENDIF // ENDIF // // only include filenames here (and do by design not include directories) // IF ( NOT ( directoryB ) ) // // do something // IF ( Lower( fileNameSearchS ) == Lower( ffNameS ) ) // AddLine( Format( AddTrailingSlash( directoryS ), ffNameS ), bufferI ) // ENDIF // ENDIF // UNTIL ( NOT ( FindNextFile( searchHandleI, -1 ) ) ) // FindFileClose( searchHandleI ) // ENDIF // END
--- cut here: end ---------------------------------------------------- 2. -Input a filename to search 1. -E.g. --- cut here: begin --------------------------------------------------
ddd1.txt
--- cut here: end ---------------------------------------------------- 3. -Input a directory name 1. -E.g. --- cut here: begin --------------------------------------------------
C:\TEMP\DDD2\
--- cut here: end ---------------------------------------------------- 4. -Run the program 5. -That will show a screen output similar to the following: (thus all filenames with the same name (=ddd1.txt) in that directory and its subdirectories) --- cut here: begin --------------------------------------------------
c:\temp\ddd2\DDD1\ddd1.txt c:\temp\ddd2\ddd1.txt
--- cut here: end ---------------------------------------------------- === Book: see also: === Diagram: see also: === File: see also: === File: version: control: see also: === Help: see also: === Image: see also: === Internet: see also: --- Computer: Editor: Text: TSE: Directory: File: Directory: All: Operation: Get: Walk: The: Tree: Link: Can you give an overview of links? http://goo.gl/Iv38p === Podcast: see also: === Record: see also: === Screencast: see also: === Table: see also: === Video: see also: === <version>1.0.0.0.2</version> ----------------------------------------------------------------------

Share |

This web page is created and maintained using the Semware TSE text editor