FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Folders in a folder
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Folders in a folder
Posted: Tue Jun 29, 2010 03:01 PM

Hello,

The function "Directory()" reads the names and data of the files in a certain folder.

But how can we read the names of all the folders which can be found in a certain folder.

Thank you very much in advance.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Folders in a folder
Posted: Tue Jun 29, 2010 03:39 PM

Add "D" as the second parameter.

EMG

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Folders in a folder
Posted: Tue Jun 29, 2010 05:11 PM
Code (fw): Select all Collapse
/*
Purpose: Get list of folders (directories)
Author : James Bott, <!-- e --><a href="mailto:jbott@compuserve.com">jbott@compuserve.com</a><!-- e -->
Syntax : getFolders( <cPath> )
         cPath defaults to current directory
Returns: Array of directory names, or empty array if no directories exist
Bug    : A filename with no extension will be
         included as a directory even though it is not.

*/

#include "fivewin.ch"

//--- Test only
function main()

   local aFolders:= getFolders( "c:\" )
   local cString:= ""
   local i:=0

   for i=1 to len( aFolders )
      cString := cString + aFolders[i] + chr(10)
   next

   msgInfo( cString )

return nil
//--- end test



function getFolders( cPath )
   local aFiles := {}
   local aFolders:={}
   local i:=0

   default cPath:= ""

   cPath:= if( right(cPath,1)="\", cPath + "*.", cPath + "\*." )

   aFiles := directory( cPath, 'D' )

   // We start at 3 to eliminate the always present "." and ".." directories
   for i=3 to len(aFiles)
      aadd( aFolders, aFiles[i,1] )
   next

return aFolders

// EOF
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Folders in a folder
Posted: Wed Jun 30, 2010 12:25 AM

James,
Enrico,

Thanks a lot for your answers.

I should have looked better to the syntax.

Sorry for disturbing you.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: Folders in a folder
Posted: Wed Jun 30, 2010 12:49 AM
Hello Mr.James,

Here I have a small workaround to avoid the bug you have mentioned in the Example.

Code (fw): Select all Collapse
#include "fivewin.ch"

//--- Test only
function main()

   local aFolders:= getFolders( "c:\" )
   local cString:= ""
   local i:=0

   for i=1 to len( aFolders )
      cString := cString + aFolders[i] + chr(10)
   next

   msgInfo( cString )

return nil
//--- end test

function getFolders( cPath )
   local aFiles := {}
   local aFolders:={}
   local i:=0
   local cOldPath := cPath                        // Added by RAMESH BABU P

   default cPath:= ""

   cPath:= if( right(cPath,1)="\", cPath + "*.", cPath + "\*." )

   aFiles := directory( cPath, 'D' )

   // We start at 3 to eliminate the always present "." and ".." directories
   for i=3 to len(aFiles)
       if IsDirectory(cOldPath+"\"+aFiles[i,1])   // Added by RAMESH BABU P
          aadd( aFolders, aFiles[i,1] )
       endif 
   next

return aFolders

// EOF


Regards,

- Ramesh Babu P
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Folders in a folder
Posted: Wed Jun 30, 2010 12:56 AM

Good idea Ramesh. I wish I had thought of that.

Thanks,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Folders in a folder
Posted: Wed Jun 30, 2010 01:52 AM
I cleaned up the code and fixed a problem with paths with trailing backslashes. This also has Ramesh's bug fix.

James

Code (fw): Select all Collapse
/*
Purpose: Get list of folders (directories)
Author : James Bott, <!-- e --><a href="mailto:jbott@compuserve.com">jbott@compuserve.com</a><!-- e -->

Syntax : getFolders( <cPath> )
         cPath defaults to current directory
Returns: Array of directory names, or empty array if no directories exist

Note: Thanks Ramesh for the bug fix!
*/

#include "fivewin.ch"

//--- Test only
function main()

   local aFolders:= getFolders( "c:\downloads" )
   local cString:= ""
   local i:=0

   for i=1 to len( aFolders )
      cString := cString + aFolders[i] + chr(10)
   next

   msgInfo( cString )

return nil
//--- end test

function getFolders( cPath )
   local aFiles := {}
   local aFolders:={}
   local i:=0

   default cPath:= ""
   cPath:= if( right(cPath,1)="\", cPath , cPath + "\" )

   aFiles := directory( cPath + "*.", 'D' )

   // We start at 3 to eliminate the always
   // present "." and ".." directories
   for i=3 to len(aFiles)
       if IsDirectory(cPath + aFiles[i,1]) // Added by RAMESH BABU P
          aadd( aFolders, aFiles[i,1] )
       endif
   next

return aFolders

// EOF
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion