Hi,
I use copyfile() to copy a single file.
Is there a function to copy a complete directory?
Thanks
Marc Vanzegbroeck wrote:Hi,
I use copyfile() to copy a single file.
Is there a function to copy a complete directory?
Thanks
/* *************************** */
#include "Directry.ch"
FUNCTION lCopyFolder( AMPAARRA, cFolderOrig, cFolderDest, lAllFolder )
/* Les carpetes es reben acabades en "\".
----------------------------------- */
Local lResposta := .T.
Local nContador := 0
Local aFiles := {}
Local cDummy1 := ""
Local cDummy2 := ""
//Traza( 1, "cFolderOrig=", cFolderOrig )
//Traza( 1, "cFolderDest=", cFolderDest )
//lMakeDirectorio( cFolderDest, .F., .T. )
/* Fitxers de la carpeta origen incloses les subcarpetes i els seus fitxers.
---------------------------------------------------------------------- */
aFiles := Directory( cFolderOrig + "*.*" , "D" )
For nContador := 1 To Len( aFiles )
//Traza( 1, cFolderOrig + aFiles[ nContador ][ F_NAME ] )
If aFiles[ nContador ][ F_ATTR ] = "D" // És un directori
//Traza( 1, "Directori." )
If lAllFolder
If .Not. ( aFiles[ nContador ][ F_NAME ] $ ".." )
//Traza( 1, aFiles[ nContador ][ F_NAME ] + "\" )
//Traza( 1, SubStr( aFiles[ nContador ][ F_NAME ] + "\", Len( cFolderOri ) + 1 ) )
//Traza( 1, cFolderDest + SubStr( aFiles[ nContador ][ F_NAME ] + "\", Len( cFolderOri ) + 1 ) )
// lMakeDirectorio( cFolderDest + aFiles[ nContador ][ F_NAME ] + "\", .F., .T. )
lResposta := lCopyFolder( AMPAARRA, ;
cFolderOrig + aFiles[ nContador ][ F_NAME ] + "\", ;
cFolderDest + aFiles[ nContador ][ F_NAME ] + "\", ;
lAllFolder )
Endif
EndIf
Else
cDummy1 := cFolderOrig + aFiles[ nContador ][ F_NAME ]
cDummy2 := cFolderDest + aFiles[ nContador ][ F_NAME ]
/*
Msgnowait( AMPAarra, ;
GetTrad("Esperi uns moments, copiant a " ) + FilePath( cDummy2, "\", 1 ) + " ...", ;
GetTrad("Copiant fitxer de: " ) + cDummy1 + CRLF + GetTrad("a: " ) + cDummy2 )
*/
Sysrefresh()
If .not. COPYFILE( (cDummy1), (cDummy2), .F. )
/*GenError( 2, GetTrad("Problemes al copiar DE " ) + (cDummy1) )
GenError( 2, GetTrad(" A " ) + (cDummy2) )
*/
lResposta := .F.
EndIf
EndIf
Endfor
Return lResposta
/* *************************** */
oFs:CopyFolder( cSourceFolder\*.*, cDestFolder, lOverWrite )
function TestCopyFolder()
local cSrcFolder := "c:\fwh\bitmaps\"
local cDstFolder := "c:\myimages\fwh\"
local oFs, lCopied := .f.
oFs := FileSysObj()
if oFs:FolderExists( cDstFolder ) .or. ;
lMkFullPath( cDstFolder )
//
TRY
oFs:CopyFolder( cSrcFolder + "*.*", cDstFolder, .t. )
lCopied := .t.
CATCH
END
endif
? lCopied
return nil
oFS := CreateObject( "Scripting.FileSystemObject" )
HB_FUNC( SHFILE )
{
#ifndef _WIN64
HWND hWnd = ( HWND ) hb_parnl( 1 );
#else
HWND hWnd = ( HWND ) hb_parnll( 1 );
#endif
SHFILEOPSTRUCT sh;
memset( ( char * ) &sh, 0, sizeof( sh ) );
sh.hwnd = hWnd ;
sh.wFunc = ( UINT ) hb_parnl( 2 );
sh.pFrom = ( LPSTR ) hb_parc( 3 );
sh.pTo = ( LPSTR ) hb_parc( 4 );
sh.fFlags = ( FILEOP_FLAGS ) hb_parnl( 5 );
sh.hNameMappings = 0;
sh.lpszProgressTitle = NULL;
hb_retnl( SHFileOperation( &sh ) );
}
FUNCTION ShellFiles( hWnd, acFiles, acTarget, nFunc, fFlag )
LOCAL cTemp
LOCAL cx
LOCAL lRet := .T.
// SourceFiles, convert Array to String
DEFAULT acFiles TO CHR( 0 )
IF VALTYPE( acFiles ) == "A"
cTemp := ""
FOR cx := 1 TO LEN( acFiles )
cTemp += acFiles[ cx ] + CHR( 0 )
NEXT
acFiles := cTemp
ENDIF
acFiles += CHR( 0 )
// TargetFiles, convert Array to String
DEFAULT acTarget TO CHR( 0 )
IF VALTYPE( acTarget ) == "A"
cTemp := ""
FOR cx := 1 TO LEN( acTarget )
cTemp += acTarget[ cx ] + CHR( 0 )
NEXT
acTarget := cTemp
ENDIF
acTarget += CHR( 0 )
// call SHFileOperation
DO CASE
CASE nFunc = FO_COPY
lRet := SHFile( hWnd, FO_COPY, acFiles, acTarget, fFlag )
CASE nFunc = FO_MOVE
lRet := SHFile( hWnd, FO_MOVE, acFiles, acTarget, fFlag )
CASE nFunc = FO_DELETE
lRet := SHFile( hWnd, FO_DELETE, acFiles, acTarget, fFlag )
CASE nFunc = FO_RENAME
lRet := SHFile( hWnd, FO_RENAME, acFiles, acTarget, fFlag )
ENDCASE
RETURN lRet
FUNCTION DoSH3func( cAction, lConfirm, lPaperbin, acFiles, acTarget, nSourceSide )
LOCAL nFocus := nSourceSide
LOCAL iMax := LEN( acFiles )
LOCAL lRet := .F.
LOCAL nHWnd, nFunc, fFlag
fFlag := FOF_SIMPLEPROGRESS
DO CASE
CASE cAction = "COPY"
nFunc := FO_COPY
CASE cAction = "MOVE"
nFunc := FO_MOVE
CASE cAction = "DELETE"
nFunc := FO_DELETE
CASE cAction = "RENAME"
nFunc := FO_RENAME
CASE cAction = "ZIPFILE"
nFunc := 0
ENDCASE
IF iMax > 1
fFlag := nOr( fFlag, FOF_MULTIDESTFILES )
ENDIF
IF lPaperbin
fFlag := nOr( fFlag, FOF_ALLOWUNDO )
ENDIF
IF lConfirm
ELSE
// fFlag += FOF_NOCONFIRMATION + FOF_NOCONFIRMMKDIR + FOF_RENAMEONCOLLISION
fFlag := nOr( fFlag, FOF_NOCONFIRMATION, FOF_NOCONFIRMMKDIR )
ENDIF
// ===========================================================================
// Function ShellFile( hParentWnd, aFiles, aTarget, nFunc, nFlag )
//
// Purpose:
// Performs a copy, move, rename, or delete operation on a file system object.
// Parameters:
// aFiles is an Array of Source-Filenamestrings, or a single Filenamestring
// aTarget is an Array of Target-Filenamestrings, or a single Filenamestring
// nFunc determines the action on the files:
// FO_MOVE, FO_COPY, FO_DELETE, FO_RENAME
// fFlag Option Flag ( see the file SHELL32.CH )
//
// ===========================================================================
lRet := ShellFiles( nHWnd, acFiles, acTarget, nFunc, fFlag )
RETURN lRet
nageswaragunupudi wrote:Nice.
Can you please show an example usage of the function ShellFiles(...) to copy a folder like this?
c:\fwh\bitmaps\*.* (including sub-folders)
to
c:\myimages\fwh\ (create folders if they do not exist)
Start_SHfunc( {"bitmaps",0,DATE(),TIME(),"D"}, "COPY", "c:\fwh\", "c:\myimages\fwh\", .F., .F. )
FUNCTION Start_SHfunc( aItem, cAction, cSourceDir, cTargetDir, lConfirm, lPaperbin )
LOCAL ii, iMax
LOCAL cFile, cAttr
LOCAL acFiles := {}
LOCAL acTarget := {}
LOCAL lRet := .F.
iMax := LEN( aItem )
FOR ii := 1 TO iMax
cFile := aItem[ ii ] [ F_NAME ]
cAttr := aItem[ ii ] [ F_ATTR ]
AADD( acFiles, cSourceDir + cFile )
IF cAction = "DELETE"
AADD( acTarget, "" + CHR( 0 ) )
ELSE
AADD( acTarget, cTargetDir + cFile )
ENDIF
NEXT
lRet := DoSH3func( cAction, lConfirm, lPaperbin, acFiles, acTarget )
RETURN lRet
Marc Venken wrote:Is this now running for standard FWH or for your Software Tools ?
DEFAULT acFiles TO
DEFAULT acFiles :=
LOCAL aItem := ::oGrid:Getdata()
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 78 guests