Folders in a folder

Folders in a folder

Postby driessen » Tue Jun 29, 2010 3: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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Folders in a folder

Postby Enrico Maria Giordano » Tue Jun 29, 2010 3:39 pm

Add "D" as the second parameter.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8570
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Folders in a folder

Postby James Bott » Tue Jun 29, 2010 5:11 pm

Code: Select all  Expand view
/*
Purpose: Get list of folders (directories)
Author : James Bott, jbott@compuserve.com
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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Folders in a folder

Postby driessen » 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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Folders in a folder

Postby RAMESHBABU » 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: Select all  Expand view

#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
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Folders in a folder

Postby James Bott » Wed Jun 30, 2010 12:56 am

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

Thanks,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Folders in a folder

Postby James Bott » Wed Jun 30, 2010 1: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: Select all  Expand view
/*
Purpose: Get list of folders (directories)
Author : James Bott, jbott@compuserve.com

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 35 guests