×
Namespaces

Variants
Actions

Folder handling in Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This article demonstrates how to perform common folder/directory operations using the Symbian C++ CFileMan class.

Article Metadata
Platform Security
Capabilities: None (unless operations on a secure directory)
Article
Created: kiran10182 (04 May 2007)
Last edited: hamishwillee (24 Jan 2012)

Libraries:

LIBRARY     efsrv.lib bafl.lib


Header File: 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
};

Source File: FolderOpers.cpp

#include <f32file.h>
#include <bautils.h>
#include <aknnotewrappers.h>
 
#include "FolderOpers.h"
 
_LIT(KFolderPath,"\\System\\Test\\");
_LIT(KDirectoryName,"\\System\\Test\\");
 
 
void FolderOpers::CallCreateL()
{
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::CallDeleteL()
{
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::CallDeleteFolderAndFilesL()
{
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::CallDeleteFolderTreeL()
{
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::CallDeleteSpecificFilesL(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::MkDirL(const TDesC &aPath)
{
RFs fs;
User::LeaveIfError(fs.Connect());
 
TInt err=fs.MkDir(aPath);
fs.Close();
return err==KErrNone;
}
 
TBool FolderOpers::RmDirL(const TDesC &aPath)
{
RFs fs;
User::LeaveIfError(fs.Connect());
 
TInt err=fs.RmDir(aPath);
fs.Close();
return err==KErrNone;
}
 
TBool FolderOpers::RmDirAndFilesL(const TDesC &aDirName)
{
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
 
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
 
TInt err=fileMan->RmDir(aDirName);
 
CleanupStack::PopAndDestroy(2, &fs); // fileMan, fs
 
return err==KErrNone;
}
 
TBool FolderOpers::RmSpecificFilesL(TDesC& aWild)
{
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
 
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
 
TInt err=fileMan->Delete(aWild);
 
CleanupStack::PopAndDestroy(2, &fs); // fileMan, fs
 
return err==KErrNone;
}
  • Paste FolderOpers.h header file in your inc folder.
  • Paste FolderOpers.cpp source file in your src folder.
  • Include this FolderOpers.h in your CYrAppUi.cpp file. Like:
#include "FolderOpers.h"
  • Finally call these functions from the HandleCommand() from CYrAppUi.Cpp file. Like:
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 24 January 2012, at 08:13.
60 page views in the last 30 days.
×