×
Namespaces

Variants
Actions

Operações em pastas

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por ivocalado
Última alteração feita por hamishwillee em 16 Aug 2012

Nos trechos de código abaixo são apresentadas diferentes funções para realização de operações que podem ser aplicadas em direrórios, usando Symbian C++


Bibliotecas:

LIBRARY efsrv.lib bafl.lib


Cabeçalho: FolderOpers.h

#include <e32base.h>
 
class FolderOpers
{
public:
static void CallCreate();
static void CallDelete();
static void CallDeleteFolderAndFiles();
static void CallDeleteFolderTree();
static void CallIsFolder();
static void CallDeleteSpecificFiles(TDesC& aWild);
 
static TBool MkDir(const TDesC &aPath); //RFs
 
//The directory must be empty and cannot be the root directory.
static TBool RmDir(const TDesC &aPath); //RFs
 
//The directory may not be empty and can remove all sub files too
static TBool RmDirAndFiles(const TDesC &aDirName); // CFileMan
 
static TBool RmSpecificFiles(TDesC& aWild); // CFileMan
};

Fonte: FolderOpers.cpp

#include <f32file.h>
#include <bautils.h>
#include <aknnotewrappers.h>
 
#include "FolderOpers.h"
 
_LIT(KFolderPath,"\\System\\Test\\");
_LIT(KDirectoryName,"\\System\\Test\\");
 
 
void FolderOpers::CallCreate()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<50folderPath;
folderPath.Copy(KFolderPath);
 
if(MkDir(folderPath))
info->ExecuteLD(_L("Folder Created"));
else
info->ExecuteLD(_L("Folder Not Created"));
}
 
void FolderOpers::CallDelete()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<50folderPath;
folderPath.Copy(KFolderPath);
 
if(RmDir(folderPath))
info->ExecuteLD(_L("Folder Deleted"));
else
info->ExecuteLD(_L("Folder Not Deleted"));
}
 
void FolderOpers::CallDeleteFolderAndFiles()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<50directoryPath;
directoryPath.Copy(KDirectoryName);
 
if(RmDirAndFiles(directoryPath))
info->ExecuteLD(_L("Folder n Files Deleted"));
else
info->ExecuteLD(_L("Folder n Files Not Deleted"));
}
 
void FolderOpers::CallDeleteFolderTree()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<50directoryPath;
directoryPath.Copy(KDirectoryName);
 
if(RmDirAndFiles(directoryPath))
info->ExecuteLD(_L("FolderTree Deleted"));
else
info->ExecuteLD(_L("FolderTree Not Deleted"));
}
 
void FolderOpers::CallDeleteSpecificFiles(TDesC& aWild)
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
 
TBuf16<50folderPath;
folderPath.Copy(KDirectoryName);
folderPath.Append(aWild);
 
if(RmSpecificFiles(folderPath))
info->ExecuteLD(_L("Files Deleted"));
else
info->ExecuteLD(_L("Files Not Deleted"));
}
 
TBool FolderOpers::MkDir(const TDesC &aPath)
{
RFs fs;
fs.Connect();
 
TInt err=fs.MkDir(aPath);
fs.Close();
return err==KErrNone;
}
 
TBool FolderOpers::RmDir(const TDesC &aPath)
{
RFs fs;
fs.Connect();
 
TInt err=fs.RmDir(aPath);
fs.Close();
return err==KErrNone;
}
 
TBool FolderOpers::RmDirAndFiles(const TDesC &aDirName)
{
RFs fs;
fs.Connect();
 
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
 
TInt err=fileMan->RmDir(aDirName);
 
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
 
return err==KErrNone;
}
 
TBool FolderOpers::RmSpecificFiles(TDesC& aWild)
{
RFs fs;
fs.Connect();
 
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
 
TInt err=fileMan->Delete(aWild);
 
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
 
return err==KErrNone;
}
  • Cole o arquivo de cabeçalho FolderOpers.h no diretório inc.
  • Cole o arquivo FolderOpers.cpp no diretório src .
  • Inclua o arquivo FolderOpers.h no arquivo CYrAppUi.cpp , da seguinte forma:
#include "FolderOpers.h"
  • Finalmente chame essas funções da função HandleCommand() do arquivo CYrAppUi.Cpp, como a seguir:
void CYrAppUi::HandleCommandL( TInt aCommand )
{
switch( aCommand )
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
 
case ELFolderOperaionsCreateFolder:
FolderOpers::CallCreate();
break;
 
case ELFolderOperaionsDeleteFolder:
FolderOpers::CallDelete();
break;
 
case ELFolderOperaionsDeleteFolderAndFiles:
FolderOpers::CallDeleteFolderAndFiles();
break;
 
case ELFolderOperaionsDeleteFolderTree:
FolderOpers::CallDeleteFolderTree();
break;
 
case ELFolderOperationsDeleteSpecificFiles:
FolderOpers::CallDeleteSpecificFiles(_L("*.xml"));
break;
 
default:
break;
}
}
This page was last modified on 16 August 2012, at 06:57.
66 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×