************************************************************************
FUNCTION CopyTree
*****************
***  Function: Copies a directory tree with wild card expression.
***            Any files in subdirectories are copied and all 
***            directories including empty ones are created.
***      Pass: lcSource -  Source Path and File Template (c:\temp\*.*)
***            lcTarget -  Target Directory to move trees to
***    Return: nothing
*************************************************************************
LPARAMETERS lcSource, lcTarget
LOCAL x

lcSourcePath = ADDBS(JUSTPATH(lcSource))
lcTargetPath = ADDBS(lcTarget)
lcSourceFiles = JUSTFNAME(lcSource)
lcTargetFiles = JUSTFNAME(lcTarget)

lcAlias = ALIAS()
SELECT 0
CREATE CURSOR TDirs( Path c(128) )

GetDirs(ADDBS(lcSourcePath),lcSourcePath)

SCAN
   IF !Directory(lcTargetPath + TRIM(path))
      MD (lcTargetPath + TRIM(Path))
   ENDIF
    
   *** Check for files first
   IF EMPTY(SYS(2000,lcSourcePath + TRIM(Path) + lcSourceFiles) )
      LOOP
   ENDIF
    
   COPY FILES (lcSourcePath + TRIM(Path)  + lcSourceFiles) TO ;
              (lcTargetPath + TRIM(Path) + lcSourceFiles)
ENDSCAN

USE 

IF !EMPTY(lcAlias) AND USED(lcAlias)
   SELECT (lcALias)
ENDIF

RETURN

************************************************************************
* DeleteTree
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION DeleteTree(lcSource)
PRIVATE lcSourcePath
LOCAL llError, loEval, lcCode, lcAlias

lcSourcePath = ADDBS(lcSource)
llError = .F.

lcCurPath = SYS(5)+CURDIR()
lcAlias = ALIAS()
CREATE CURSOR TDirs( Path c(128) )


GetDirs(ADDBS(lcSourcePath),lcSourcePath)
INDEX ON Path DESCENDING TAG PATH
IF _TALLY < 1
   RETURN .F.
ENDIF

SCAN
   lcPath = lcSourcePath + TRIM(path)
   ERASE (lcPath + "*.*")
   IF ISDir(lcPath)
      RD (lcPath)
   ENDIF
ENDSCAN


RETURN !llError
ENDFUNC
* DeleteTree

*******************************
FUNCTION GetDirs(lcPath,lcBase)
*******************************
LOCAL x, lnFiles, laDirs[1]

INSERT INTO TDirs (Path) Values ( STRTRAN(lcPath,lcbase,"") )

*** Get directories first
lnFiles = aDir(laDirs,ADDBS(lcPath) + "*." ,"D")
FOR x=1 to lnFiles
   if laDirs[x,1] = "." OR laDirs[x,1] = "    "
      LOOP
   ENDIF
   GetDirs( ADDBS(lcPath + laDirs[x,1]),lcBase ) 
ENDFOR


ENDFUNC
